Python Forum
Want to replace last column header value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Want to replace last column header value
#1
Want to replace last column header value to "Sourcefile". This python script is to extract each sheet in an Excel workbook as a new csv file.

import csv
import xlrd
import sys

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',newline='')
        writetocsv = csv.writer(csvfile, quoting = csv.QUOTE_ALL)
        for rownum in range(worksheet.nrows):
            writetocsv.writerow(
                list(str(x) for x in worksheet.row_values(rownum))+[sheet_name]            
            )          
        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 create a spreadsheet with column and row header ouruslife 4 1,607 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  How to replace column of a Matrix Joni_Engr 5 5,915 Aug-22-2021, 10:35 AM
Last Post: Joni_Engr
  How to change row 2 to column header within a dataframe sparkt 2 2,146 Aug-20-2020, 05:12 PM
Last Post: sparkt
  Search & Replace - Newlines Added After Replace dj99 3 3,391 Jul-22-2018, 01:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

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