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
#30
Thanks dean for format:
I only created an excel file with sheet1:
Spreadsheet:
   
code:
import pandas as pd
from pathlib import Path
import os 
  
  
class ExcelToCsv:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
  
    def excel_to_csv(self, workbook, sheetname, csvfile, startrow=None, endrow=None, index=False):
        # Allows for selection start/end if sepecified 
        if startrow:
            skiprows = startrow
            if startrow > 1:
                skiprows = startrow-1
            nrows = endrow - startrow
            df = pd.read_excel(workbook, sheet_name=sheetname, skiprows=skiprows, nrows=nrows)
        else:
            df = pd.read_excel(workbook, sheet_name=sheetname)
        print(df)
        df.to_csv(csvfile, index=index)

def main():
    etc = ExcelToCsv()
    # You will need to adjust filenames and locations
    just1 = Path('../data/excel/JustFirstSheet.xlsx')
    csvfile = Path('./extocsv.csv')
    etc.excel_to_csv(just1, 'Sheet1', csvfile=csvfile)

if __name__ == '__main__':
    main()
results:
Output:
Segment,Country,Product,Discount Band,Units Sold Manufacturing Price,Sale Price,Gross Sales,Discounts Sales,COGS Profit,Date,Month Number,Month Name,Year,Unnamed: 13,Unnamed: 14,Unnamed: 15,Unnamed: 16,Unnamed: 17,Unnamed: 18,Unnamed: 19 Government,Canada,Carretera,None,1618.5,$3.00,$20.00,$32,370,$-,$32,370,$16,185,$16,185,01/01/2014,1,January,2014 Government,Germany,Carretera,None,1321.0,$3.00,$20.00,$26,420,$-,$26,420,$13,210,$13,210,01/01/2014,1,January,2014 Midmarket,France,Carretera,None,2178.0,$3.00,$15.00,$32,670,$-,$32,670,$21,780,$10,890,01/06/2014,6,June,2014 Midmarket,Germany,Carretera,None,888.0,$3.00,$15.00,$13,320,$-,$13,320,$8,880,$4,440,01/06/2014,6,June,2014 Midmarket,Mexico,Carretera,None,2470.0,$3.00,$15.00,$37,50,$-,$37,50,$24,700,$12,350,01/06/2014,6,June,2014 Government,Germany,Carretera,None,1513.0,$3.00,$350.00,$529,550,$-,$529,550,$393,380,$136,170,01/12/2014,12,December,2014 Midmarket,Germany,Montana,None,921.0,$5.00,$15.00,$13,815,$-,$13,815,$9,210,$4,605,01/03/2014,3,March,2014 Channel Partners,Canada,Montana,None,2518.0,$5.00,$12.00,$30,216,$-,$30,216,$7,554,$22,662,01/06/2014,6,June,2014 Government,France,Montana,None,1899.0,$5.00,$20.00,$37,980,$-,$37,980,$18,990,$18,990,01/06/2014,6,June,2014 Channel Partners,Germany,Montana,None,1545.0,$5.00,$12.00,$18,540,$-,$18,540,$4,635,$13,905,01/06/2014,6,June,2014 Midmarket,Mexico,Montana,None,2470.0,$5.00,$15.00,$37,50,$-,$37,50,$24,700,$12,350,01/06/2014,6,June,2014
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 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 591 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,416 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 1,010 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Help creating shell scrip for python file marciokoko 10 1,599 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,239 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,421 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 910 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 934 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,947 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