Here's a solution (based on post #1) which kinda works (I get Sheet1.csv and Sheet2.csv corresponding to Sheet1 and Sheet2 respectively) except that I get single blank lines in between the data rows! I've fiddled with the code to see why it's doing that, but nothing works.
"file1.csv" doesn't get produced.
"file1.csv" doesn't get produced.
import csv import openpyxl def xls_to_csv(xls_name, csv_name) -> None: wb = openpyxl.load_workbook(xls_name) for sheet in wb.sheetnames: with open(f'{sheet.title()}.csv', 'w') as csv_file: writer = csv.writer(csv_file) xls_sheet = wb[sheet] maxRow = xls_sheet.max_row + 1 maxCol = xls_sheet.max_column + 1 headers = (xls_sheet.cell(row=1, column=col).value for col in range(1, maxCol)) writer.writerow(headers) for r in range(2, maxRow): xls_row = (xls_sheet.cell(row=r, column=col).value for col in range(1, maxCol)) writer.writerow(xls_row) if __name__ == '__main__': import sys import pathlib xlsfile="D:/Power BI & Python/ExcelData3.xlsx" csvfile = "file1.csv" with pathlib.Path(xlsfile) as xls_file: if xls_file.is_file(): xls_to_csv(xlsfile, csvfile)