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
#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


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 deanhystad - Oct-29-2022, 06:51 PM
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 503 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 596 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,420 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 1,019 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Help creating shell scrip for python file marciokoko 10 1,631 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,241 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,436 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 918 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 940 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 11,023 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