14

Is there any way to create a CSV file with multiple sheets programmatically in Python?

APerson
  • 8,140
  • 8
  • 35
  • 49
ianlokejj
  • 153
  • 1
  • 2
  • 5

4 Answers4

15

Multiple CSV files. One CSV file per sheet.

A comma seperated value file is a plain text format. It is only going to be able to represent flat data, such as a table (or a 'Sheet')

For storing multiple sheets, you should use separate CSV files. You can write each one separately and import/parse them individually into their destination.

skeevey
  • 611
  • 4
  • 11
6

The xlsxwriter library is what you're looking for. It can make a workbook with multiple sheets.
Look at the link for tutorials and code.

P.S I am answering this because this is the first result I got when searching on how to do this. Hopefully this helps others.

Christopher Nuccio
  • 596
  • 1
  • 9
  • 21
2

Not sure what you are trying to do with Multiple Sheets of CSV's. Let me elaborate my thinking on this.

  1. Maybe you want a folder with different CSV files in it.
  2. If you "can" use XML then probably you may want to have XML sheet in a big XML "Workbook".

Haven't seen multiple sheets of csv yet.

LonelySoul
  • 1,212
  • 5
  • 18
  • 45
1

This worked for me as a conversion from excel to CSV while exporting individual sheet names.

    xls = pd.read_excel/csv('file name.xls/csv', sheet_name =['1','2','3','4','5','6','7','8','9','10'])

    #list out the sheets  you want to export in the bracket above separated by commas and ' '

    for sheet_name, df in xls.items():
     df['sheets'] = sheet_name    
     df[['sheets']].to_csv(f'{sheet_name}.csv', header=None)
    export_csv = df.to_csv (r'Location you want to export to on your machine',
                            index = None, header=True)
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45