-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_xls_xlsx_for_csv.py
More file actions
23 lines (20 loc) · 983 Bytes
/
convert_xls_xlsx_for_csv.py
File metadata and controls
23 lines (20 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import pandas as pd
# Diretório onde estão as pastas com os arquivos .xls
diretorio = r'C:\Users\UNIR\OneDrive\Área de Trabalho\Avaliacoes'
# Função para converter arquivos .xls em .csv
def converter_xls_para_csv(pasta):
for subdir, dirs, files in os.walk(pasta):
for file in files:
if file.endswith('.xls'):
arquivo_xls = os.path.join(subdir, file)
# Ler o arquivo .xls
xls = pd.ExcelFile(arquivo_xls)
# Converter cada planilha do arquivo .xls em um arquivo .csv
for sheet_name in xls.sheet_names:
df = xls.parse(sheet_name)
csv_file = os.path.splitext(arquivo_xls)[0] + '_' + sheet_name + '.csv'
df.to_csv(csv_file, index=False)
print(f'Arquivo "{file}" convertido para CSV.')
# Chamar a função para percorrer o diretório e converter os arquivos
converter_xls_para_csv(diretorio)