Oct-29-2022, 07:22 PM
I have named it 'exceltocsv.py' but I get no csv files appearring. Here is the code I have in the two .py files.
import pandas as pd from pathlib import Path import os class ExcelToCsv: def __init__(self): os.chdir(os.path.abspath(os.path.dirname(__file__))) self.homepath = Path('.') def excel_to_csv(self, workbook, sheetname, startrow, endrow, csvfile, index=False): # Added to make sure filename is Posix compliant. if not isinstance(csvfile, 'pathlib.PosixPath'): csvfile = Path(csvfile) skiprows = startrow if startrow > 1: skiprows = startrow - 1 df = pd.read_excel(workbook, sheetname, skiprows, endrow - startrow) df.to_csv(csvfile, index=index)
from exceltocsv import ExcelToCsv from openpyxl import load_workbook ExcToCSV = ExcelToCsv().excel_to_csv def main(): workbook = r"ExcelData3.xlsx" wb = load_workbook(workbook) for ws in wb.worksheets: sheetname = ws.title csv_file = f'{ws.title}.csv' ExcToCSV(workbook, sheetname, startrow=1, nrows=ws.max_row, csvfile=csv_file) print(csv_file)