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
  Help in opening and editing an excel workbook from a web-browser test 4 264 Aug-10-2022, 02:31 PM
Last Post: test
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 292 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Read xml column inside csv file with Python estertabita 2 402 Jul-26-2022, 06:09 PM
Last Post: Larz60+
  Excel file in use from Python Mark17 7 600 Jul-04-2022, 02:06 PM
Last Post: Mark17
  How to format Excel column with comma? dee 0 306 Jun-13-2022, 10:11 PM
Last Post: dee
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 346 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  Extract parts of a log-file and put it in a dataframe hasiro 4 852 Apr-08-2022, 01:18 PM
Last Post: hasiro
  How to split file by same values from column from imported CSV file? Paqqno 5 852 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 721 Feb-11-2022, 03:18 PM
Last Post: snippsat
  Appending Excel column value as CSV file name sh1704 0 633 Feb-06-2022, 10:32 PM
Last Post: sh1704

Forum Jump:

User Panel Messages

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