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
#31
Many thanks, that worked.
Reply
#32
Here's an alternative solution which works except that I get single blank lines in between the data rows! I've fiddled with the code to see why it's doing that, but nothing works.
Reply
#33
Here's a solution (based on post #1) which kinda works (I get Sheet1.csv and Sheet2.csv corresponding to Sheet1 and Sheet2 respectively) except that I get single blank lines in between the data rows! I've fiddled with the code to see why it's doing that, but nothing works.

"file1.csv" doesn't get produced.

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'{sheet.title()}.csv', 'w') as csv_file:
            writer = csv.writer(csv_file)
            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.writerow(xls_row)

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

    xlsfile="D:/Power BI & Python/ExcelData3.xlsx"
    csvfile = "file1.csv"

    with pathlib.Path(xlsfile) as xls_file:
        if xls_file.is_file():
            xls_to_csv(xlsfile, csvfile)
Reply
#34
You stated that the solution that In Post 30 works.
So why are you not using it?
Reply
#35
It does work except that I get alternate blank lines that's all. I've fiddled with the code to get rid of the blank lines but can't figure it out.
Reply
#36
Plus the solution presented in post 13 is superior.
Reply
#37
Agreed, this is as much to do with trying to understand the code in post #33 and try to learn Python as I am a beginner.
Reply
#38
Search for python csv writer blank row. This is a well known problem with an easy fix. When opening the csv file for writing, tell it to not add newlines when writing a line.
import csv
import openpyxl
from pathlib import Path

def xls_to_csv(xls_name, csv_name=None) -> None:
    # Get folder, partial filename and suffix for csv file(s).
    # filename will be partial filename_sheet name
    path = Path(xls_name) if csv_name is None else Path(csv_name)
    suffix = ".csv" if csv_name is None else path.suffix
    folder = path.parent
    name, *_ = path.name.split(".")

    wb = openpyxl.load_workbook(xls_name)
    for sheet in wb.sheetnames:
        csv_file = folder/f"{name}_{sheet}{suffix}"
        with open(csv_file, 'w', newline="") as csv_file:
            writer = csv.writer(csv_file)
            for row in wb[sheet].values:  # Get all the values in a row
                writer.writerow(row)

if __name__ == '__main__':
    import sys
    xls_to_csv(*sys.argv[1:])
file1.csv is not produced because you never use csv_name.
Reply
#39
Thanks deanhystad, I learnt something today. The code worked.
Reply
#40
Run code in the debugger. Set breakpoints and look at the objects. That is how I found that workbook sheet has a values property. Then I tried iterating and saw it return the values for a row. You will learn a lot about Python very quickly by running in interactive mode. Writing code and only seeing the end results isolates you, makes you rely too much on documentation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 383 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 492 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,307 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 907 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Help creating shell scrip for python file marciokoko 10 1,446 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,151 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,154 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 845 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 883 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,322 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