Previously, I was getting a win32 error 'open method of excel workbooks failed'. But now, I'm getting an error in the excel macro I'm trying to run via python, 'subscript out of range'.
When I debug it, it outlines this line of code:
Sheets("2 RawData").Select
I know that the error is supposed to mean that there isn't a sheet with that name, but when I checked, it was there:
And this is a screenshot of the errors from python:
This is the code I'm trying to run to parse an excel document via macro:
if os.path.exists(self.excel_parser_location):
# print "Opening Telematics_Messages_Parser.xlsm in Excel"
xl = client.Dispatch("Excel.Application")
xl.Application.visible = False
wb = xl.Workbooks.Open(os.path.abspath(self.excel_parser_location), ReadOnly=True)
xl.Application.Run('DoThisFirst')
xl.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
# xl.Application.Run('SheetKiller')
xl.Application.Run('CleanUp')
xl.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
# If the file already exists
if os.path.exists(save_path):
# Remove the older save
os.remove(save_path)
wb.SaveAs(save_path)
# print "Saving Parsed_Messages.xlsm"
wb.Close(True)
del xl
Edit: I called this method before the method to run the macros:
if os.path.exists(csv_path):
#data = pd.read_csv(csv_path, error_bad_lines=False)
data = pd.read_csv(csv_path, on_bad_lines='skip')
book = openpyxl.load_workbook(self.excel_parser_location,keep_vba=True)
writer = pd.ExcelWriter(self.excel_parser_location)
writer.book = book
data.to_excel(writer, sheet_name='2 RawData', index=False)
# print 'Writing new data'
book.remove(book['2 RawData'])
# print 'Removing blank sheet'
book_sheet = book['2 RawData1']
book_sheet.title = '2 RawData'
# print 'Renaming sheet'
writer.save()
writer.close()
# print 'Saving Telematics_Messages_Parser.xlsm'
self.run_parsing_macro(sn)
os.remove(csv_path)
return True
It's supposed to copy data to the excel sheet from a downloaded csv file.