Mar-31-2021, 11:02 AM
(This post was last modified: Mar-31-2021, 11:02 AM by shantanu97.)
written a python code for converting excel to csv code. run code in c and g drive. error is coming.
#Purpose: '''This python script is to extract each sheet in an Excel workbook as a new csv file'''
#Usage: python ExceltoCSV.py [Excel_File_Location] [CSV_File_Location]
# [C:\Users\....\Input_File.xlsx] [C:\Users\....\Output_Folder_Name/] Remember to put Forward Slash After Output Folder Name to save 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):
if rownum == 0:
writetocsv.writerow(
list(str(x) for x in worksheet.row_values(rownum))+["SourceFile"]
)
else:
writetocsv.writerow(
list(str(x) for x in worksheet.row_values(rownum))+[excel_file.split("\\")[-1]]
)
csvfile.close()
if __name__ == '__main__':
ExceltoCSV(excel_file = sys.argv[1], csv_file_base_path = sys.argv[2])it will show error.Error: File "ExceltoCSV.py", line 29, in <module>
ExceltoCSV(excel_file = sys.argv[1], csv_file_base_path = sys.argv[2])
File "ExceltoCSV.py", line 10, in ExceltoCSV
workbook = xlrd.open_workbook(excel_file)
File "C:\Users\ShantanuGupta\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\__init__.py", line 111, in open_workbook
with open(filename, "rb") as f:
PermissionError: [Errno 13] Permission denied: 'G:\\TestFilesIn'
