summaryrefslogtreecommitdiff
path: root/Chapter14PracExcelToCsv.py
blob: c8f374fc5fd291ce3d6929785266cf98ae16c89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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()