Python Forum
Add a new column when I extract each sheet in an Excel workbook as a new csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add a new column when I extract each sheet in an Excel workbook as a new csv file
#1
Below is a python script is to extract each sheet in an Excel workbook as a new csv file.Want to Add a new column when I extract each sheet in an Excel workbook as a new csv file. The new column is added at the end of last column of every file. Can anyone help me how to do this??
'''This python script is to extract each sheet in an Excel workbook as a new csv file'''
import csv
import xlrd
import sys
import pandas as pd

def ExceltoCSV(excel_file, csv_file_base_path):
    workbook = xlrd.open_workbook(excel_file)
    for sheet_name in workbook.sheet_names():
        print('processing - ' + sheet_name)
        worksheet = workbook.sheet_by_name(sheet_name)
        csv_file_full_path = csv_file_base_path + sheet_name.lower().replace(" - ", "_").replace(" ","_") + '.csv'
        csvfile = open(csv_file_full_path, 'w')
        writetocsv = csv.writer(csvfile, quoting = csv.QUOTE_MINIMAL)
        for rownum in range(worksheet.nrows):
            writetocsv.writerow(
                list(x.encode('utf-8') if type(x) == type(u'') else x for x in worksheet.row_values(rownum)
                )
            )
            #writetocsv.writerow(rownum.append(sheet_name)) #Adding a new column
        csvfile.close()


if __name__ == '__main__':
    ExceltoCSV(excel_file = sys.argv[1], csv_file_base_path = sys.argv[2])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python script for excel sheet Nabil 5 290 Jun-01-2021, 02:29 PM
Last Post: anuradha
  Extract specific sentences from text file Bubly 3 230 May-31-2021, 06:55 PM
Last Post: Larz60+
  Filter Excel and Convert an Excel File giddyhead 0 253 May-13-2021, 06:31 PM
Last Post: giddyhead
  Problems with inserting images into an Excel File FightingFarmer 2 278 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  Copy column from one existing excel file to another file mkujawsk 0 319 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Importing text file into excel spreadsheet with formatting david_dsmn 1 313 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 513 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Need help with saving output into an excel file Beyondfacts 4 522 Mar-22-2021, 11:51 AM
Last Post: jefsummers
  Python Matplotlib: Create chart for every 4 columns in Excel file JaneTan 2 574 Feb-28-2021, 05:02 AM
Last Post: JaneTan
  Text to Column to Excel in Python giddyhead 0 321 Feb-09-2021, 03:18 AM
Last Post: giddyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020