Python Forum
Creating csv files from Excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating csv files from Excel file
#1
I want to transfer worksheets in a workbook into individual csv files per worksheet (I am experimenting with 1 worksheet at the moment). I found the following code on the internet, but I am getting errors. I see an empty file called 'file1.csvSheet1.csv'. I expected the file to be file1.csv.

import csv
import openpyxl

def xls_to_csv(xls_name, csv_name) -> None:
    wb=openpyxl.load_workbook(xls_name)

    for sheet in wb.sheetnames:
        with open(f'{csv_name}{sheet.title()}.csv','w') as csv_file:
            writer=csv.writer(csv_name)
            xls_sheet=wb[sheet]
            maxRow=xls_sheet.max_row+1
            maxCol=xls_sheet.max_column+1
            headers=(xls_sheet.cell(row=1,column=col).value for col in range(1, maxCol))
            writer.writerow(headers)

            for r in range(2,maxRow):
                xls_row=(xls_sheet.cell(row=r,column=col).value for col in range(1, maxCol))
                writer.writerrow(xls_row)

if __name__ == '__main__':
    import sys
    import pathlib

    with pathlib.Path("D:/Power BI & Python/ExcelData3.xlsx") as xls_file:
        if xls_file.is_file():
            xls_to_csv("D:/Power BI & Python/ExcelData3.xlsx","file1.csv")
"D:\Power BI & Python\venv\Scripts\python.exe" "D:/Power BI & Python/Test.py" 
Traceback (most recent call last):
  File "D:\Power BI & Python\Test.py", line 26, in <module>
    xls_to_csv("D:/Power BI & Python/ExcelData3.xlsx","file1.csv")
  File "D:\Power BI & Python\Test.py", line 9, in xls_to_csv
    writer=csv.writer(csv_name)
TypeError: argument 1 must have a "write" method

Process finished with exit code 1
Reply


Messages In This Thread
Creating csv files from Excel file - by azizrasul - Oct-27-2022, 09:37 PM
RE: Creating csv files from Excel file - by Larz60+ - Oct-28-2022, 01:25 AM
RE: Creating csv files from Excel file - by Yoriz - Oct-28-2022, 09:58 PM
RE: Creating csv files from Excel file - by Larz60+ - Oct-28-2022, 10:02 PM
RE: Creating csv files from Excel file - by Larz60+ - Oct-29-2022, 08:55 AM
RE: Creating csv files from Excel file - by Larz60+ - Oct-29-2022, 07:56 PM
RE: Creating csv files from Excel file - by Larz60+ - Oct-29-2022, 11:54 PM
RE: Creating csv files from Excel file - by Larz60+ - Nov-01-2022, 11:44 PM
RE: Creating csv files from Excel file - by Larz60+ - Nov-02-2022, 06:23 PM
RE: Creating csv files from Excel file - by Larz60+ - Nov-02-2022, 08:46 PM
RE: Creating csv files from Excel file - by Larz60+ - Nov-03-2022, 01:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 445 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 553 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,371 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 972 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Help creating shell scrip for python file marciokoko 10 1,546 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,204 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,326 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 882 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 916 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,711 Jan-21-2023, 06:57 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020