Oct-28-2022, 08:37 PM
(This post was last modified: Oct-28-2022, 08:37 PM by deanhystad.)
You have all the pieces
You know how to open an excel spreadsheet and get the sheets. <- From your program
You know how to get a sheet from a workbook and write it to a csv file. <- From Larz60+ program
You'll need to loop through the sheets in the workbook, converting a page at a time.
The only part you don't quite have is making up a good name for the CSV file(s). You had a good start with your first post, but you'll need to do some work with removing extensions and getting the sheet name. Minor details that won't take long to iron out. Look at pathlib.Path. With Path it is easy to get directory, filename and extension from a file path.
I don't know that Excel_to_Csv needs a class. I think some boilerplate got copied over from a project where you want to change the working directory (maybe making zip files). Not needed here.
Some of the arguments to excel_to_csv could also use default values. Most of the time you want to convert the entire sheet and many workbooks have a single sheet. I made a few small changes.
You know how to open an excel spreadsheet and get the sheets. <- From your program
You know how to get a sheet from a workbook and write it to a csv file. <- From Larz60+ program
You'll need to loop through the sheets in the workbook, converting a page at a time.
The only part you don't quite have is making up a good name for the CSV file(s). You had a good start with your first post, but you'll need to do some work with removing extensions and getting the sheet name. Minor details that won't take long to iron out. Look at pathlib.Path. With Path it is easy to get directory, filename and extension from a file path.
I don't know that Excel_to_Csv needs a class. I think some boilerplate got copied over from a project where you want to change the working directory (maybe making zip files). Not needed here.
Some of the arguments to excel_to_csv could also use default values. Most of the time you want to convert the entire sheet and many workbooks have a single sheet. I made a few small changes.
def excel_to_csv(workbook, csvfile=None, sheet=0, start=None, end=None, index=False): """Convert excel spreadsheet (.xlsx) to csv file workbook : Name of excel file to read. csvfile : Name of csv file to write. Default to workbook with .csv extension sheet : Name of sheet in workbook to convert. Defaults to first sheet start : Start reading at this row. Default is start reading at row 0. end : Stop reading at this row. Default is read to end. index : Write index column if True. Default is False """ if csvfile is None: csvfile = Path(workbook).with_suffix(".csv") skiprows = max(0, start - 1) if start is not None else None nrows = end - start if start and end else None df = pd.read_excel(workbook, sheet_name=sheet, skiprows=skiprows, nrows=nrows) df.to_csv(csvfile, index=index)