summaryrefslogtreecommitdiff
path: root/Chapter14PracExcelToCsv.py
diff options
context:
space:
mode:
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()