summaryrefslogtreecommitdiff
path: root/Chapter14PracExcelToCsv.py
diff options
context:
space:
mode:
authormlot <petri-rush-curvy@duck.com>2025-06-06 13:40:57 -0400
committermlot <petri-rush-curvy@duck.com>2025-06-06 13:40:57 -0400
commit75a42ec54dbf721caa659ddf02c1f46fc2cb4bef (patch)
tree84be794a2481e356a7784557a6f9fb6fbf29cfdd /Chapter14PracExcelToCsv.py
initial commit for archivingHEADmain
Diffstat (limited to 'Chapter14PracExcelToCsv.py')
-rw-r--r--Chapter14PracExcelToCsv.py26
1 files changed, 26 insertions, 0 deletions
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()