From 75a42ec54dbf721caa659ddf02c1f46fc2cb4bef Mon Sep 17 00:00:00 2001 From: mlot Date: Fri, 6 Jun 2025 13:40:57 -0400 Subject: initial commit for archiving --- Chapter14PracExcelToCsv.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Chapter14PracExcelToCsv.py (limited to 'Chapter14PracExcelToCsv.py') diff --git a/Chapter14PracExcelToCsv.py b/Chapter14PracExcelToCsv.py new file mode 100644 index 0000000..c8f374f --- /dev/null +++ b/Chapter14PracExcelToCsv.py @@ -0,0 +1,26 @@ +#! python3 + +# Chapter 14 Practice Excel to CSV Converter +# Converts all Excel file sheets in the current directory to CSV files + +import os +import csv +import openpyxl + +for excelFile in os.listdir('.'): + if excelFile.endswith('.xlsx'): + wb = openpyxl.load_workbook(excelFile) + for sheetName in wb.get_sheet_names(): + sheet = wb.get_sheet_by_name(sheetName) + csvFileName = open(excelFile + sheetName + '.csv', 'w', newline='') + csvFile = csv.writer(csvFileName) + + for rowNum in range(1, sheet.get_highest_row() + 1): + rowData = [] + for colNum in range(1, sheet.get_highest_column() + 1): + cellData = sheet.cell(row=rowNum, column=colNum).value + rowData.append(cellData) + + csvFile.writerow(rowData) + + csvFileName.close() -- cgit