Spaces:
Sleeping
Sleeping
File size: 1,377 Bytes
91d7875 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import openpyxl
import csv
import tempfile
def xlxs_to_csv(file_path: str, sheet_name: str = None) -> list[str]:
"""
Convert a workbook into a list of csv files
:param file_path: the path to the workbook
:param sheet_name: the name of the sheet to convert
:return: a list of temporary file names
"""
# Load the workbook and select the active worksheet
wb = openpyxl.load_workbook(file_path)
# ws = wb.active
#
# # Create a new temporary file and write the contents of the worksheet to it
# with tempfile.NamedTemporaryFile(mode='w+', newline='', delete=False) as f:
# c = csv.writer(f)
# for r in ws.rows:
# c.writerow([cell.value for cell in r])
# return f.name
# load all sheets if sheet_name is None
wb = wb if sheet_name is None else [wb[sheet_name]]
temp_file_name = []
# Iterate over the worksheets in the workbook
for ws in wb:
# Create a new temporary file and write the contents of the worksheet to it
with tempfile.NamedTemporaryFile(mode='w+', newline='', suffix='.csv', delete=False) as f:
c = csv.writer(f)
for r in ws.rows:
c.writerow([cell.value for cell in r])
temp_file_name.append(f.name)
# print(f'all Sheets are saved to temporary file {temp_file_name}')
return temp_file_name
|