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
#11
Note: Using a function allows you to call the code for other files.

Better yet, keep the code I sent in a separate script. This can now be imported and caled as needed.
You can also make modifications to the class, and they are automatically available in the scripts that import that class.
Using a modification your final code. I will show how this is done below.
It's what you should strive for to keep code modular.

first save my code as ExcelToCSV.py
import pandas as pd
from pathlib import Path
import os 
 
 
class ExcelToCsv:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.homepath = Path('.')
 
    def excel_to_csv(self, workbook, sheetname, startrow, endrow, csvfile, index=False):
        # Added to make sure filename is Posix compliant.
        if not isinstance(csvfile, 'pathlib.PosixPath'):
            csvfile = Path(csvfile)
        skiprows = startrow
        if startrow > 1:
            skiprows = startrow-1
        df = pd.read_excel(workbook, sheet_name=sheetname, skiprows, nrows=endrow-startrow)
        df.to_csv(csvfile, index=index)
your modified code:
from ExcelToCsv import ExcelToCsv
from openpyxl import load_workbook

ExcToCSV = ExcelToCsv().excel_to_csv

def main():
    workbook = r"ExcelData3.xlsx"
    wb = load_workbook(workbook)
    for ws in wb.worksheets:
        sheetname=ws.title
    csv_file=f'{ws.title}.csv'
    ExcToCSV(workbook, sheetname, startrow=1, nrows = ws.max_row)
Not tested.
Reply
#12
Done that, but I get the following error: -

"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 1, in <module>
    from ExcelToCsv import ExcelToCsv
ModuleNotFoundError: No module named 'ExcelToCsv'

Process finished with exit code 1
Reply
#13
What did you name the module (file)? Remember that case is important, and convention is to use all lower case for modules.

I would package it like this:
# shebang?

import openpyxl
import pandas as pd
from pathlib import Path

def read_excel_sheet(workbook, sheet=0, start=None, end=None):
    """Read sheet from excel workbook.  Return dataframe
 
    workbook : Name of excel file to read.
    sheet    : Name of sheet in workbook to convert.  Defaults to first sheet
    start    : Start reading at this row.  Default is start reading at row 0.
    end      : Stop reading at this row.  Default is read to end.
    """
    skiprows = max(0, start - 1) if start is not None else None
    nrows = end - start if start and end else None
    return pd.read_excel(workbook, sheet_name=sheet, skiprows=skiprows, nrows=nrows)


def excel_sheet_to_csv(workbook, sheet=0, csvfile=None, start=None, end=None, index=False):
    """Convert excel spreadsheet (.xlsx) to csv file
 
    workbook : Name of excel file to read.
    sheet    : Name of sheet in workbook to convert.  Defaults to first sheet
    csvfile  : Name of csv file to write.  Default to workbook with .csv extension
    start    : Start reading at this row.  Default is start reading at row 0.
    end      : Stop reading at this row.  Default is read to end.
    index    : Write index column if True.  Default is False
    """
    if csvfile is None:
        csvfile = Path(workbook).with_suffix(".csv")
    df = read_excel_sheet(workbook, sheet, start, end)
    df.to_csv(csvfile, index=index)


def excel_to_csv(workbook, csvfile=None):
    """Convert excel workbook (.xlsx) to csv file(s)
 
    workbook : Name of excel file to read.
    csvfile  : Name of csv file to write.  Default to workbook with .csv extension
    """
    if csvfile is None:
        csvfile = Path(workbook).with_suffix(".csv")

    sheets = openpyxl.load_workbook(workbook).sheetnames
    if len(sheets) == 1:
        excel_sheet_to_csv(workbook, 0, csvfile)
    else:
        path = Path(csvfile)
        name, ext = path.name.split(".")
        for sheet in sheets:
            excel_sheet_to_csv(
                workbook,
                sheet,
                path.parent/f"{name}_{sheet}.{ext}")


if __name__ == "__main__":
    import sys
    excel_to_csv(*sys.argv[1:])
This gives you the same excel_to_csv function that you already have (now called excel_sheet_to_csv). It gives you a new function (named excel_to_csv) that converts all the sheets in a workbook to csv files and another that reads a sheet from a workbook and returns it as a dataframe.

And for good measure, you can add a shebang, pass in command line arguments, and run it like a program.
Reply
#14
I have named it 'exceltocsv.py' but I get no csv files appearring. Here is the code I have in the two .py files.

import pandas as pd
from pathlib import Path
import os

class ExcelToCsv:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.homepath = Path('.')

    def excel_to_csv(self, workbook, sheetname, startrow, endrow, csvfile, index=False):
        # Added to make sure filename is Posix compliant.
        if not isinstance(csvfile, 'pathlib.PosixPath'):
            csvfile = Path(csvfile)
        skiprows = startrow
        if startrow > 1:
            skiprows = startrow - 1
        df = pd.read_excel(workbook, sheetname, skiprows, endrow - startrow)
        df.to_csv(csvfile, index=index)
from exceltocsv import ExcelToCsv
from openpyxl import load_workbook

ExcToCSV = ExcelToCsv().excel_to_csv

def main():
    workbook = r"ExcelData3.xlsx"
    wb = load_workbook(workbook)
    for ws in wb.worksheets:
        sheetname = ws.title
    csv_file = f'{ws.title}.csv'
    ExcToCSV(workbook, sheetname, startrow=1, nrows=ws.max_row, csvfile=csv_file)
    print(csv_file)
Reply
#15
From post #13 I get

  File "D:\Power BI & Python\Test.py", line 60, in <module>
    excel_to_csv(workbook,*sys.argv[1:])
NameError: name 'workbook' is not defined
Reply
#16
The class module should be saved as ExcelToCsv.py
Capital letters (which are expected (but not required) for class scripts)
Reply
#17
Changed the .py file as suggested and I get exit 0 but no output files!
Reply
#18
Not sure what 'exit 0' is.
Since I can't see what your input file looks like, I cannot test.
I would use debugger and single step through if able.
do you know how to use debugger?
Reply
#19
(Oct-29-2022, 07:28 PM)azizrasul Wrote: From post #13 I get

  File "D:\Power BI & Python\Test.py", line 60, in <module>
    excel_to_csv(workbook,*sys.argv[1:])
NameError: name 'workbook' is not defined
Operator error? Did you provide a filename for the workbook argument?
test workbook_filename optional_csv_filename
or
python test.py workbook_filename optional_csv_filename
Reply
#20
'exit 0' in PyCharm means there was no error.
Reply


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