Reputation: 31
So, I am using openpyxl and pandas to open an excel file, and write in data. Then, I'm attempting to use pywin32 to open the same file and run a macro to parse the data. But, I'm getting this error when attempting to open the file with pywin32:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)
This is the code I'm using with pywin32:
if os.path.exists(self.excel_parser_location):
# print "Opening Telematics_Messages_Parser.xlsm in Excel"
xl = client.Dispatch("Excel.Application")
xl.Application.visible = True
wb = xl.Workbooks.Open(os.path.abspath(self.excel_parser_location), ReadOnly=1)
And this is the code I'm using to write in the data before using pywin32:
if os.path.exists(csv_path):
data = pd.read_csv(csv_path, error_bad_lines=False)
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()
I've had a similar issue in the past that I resolved by using an older version of pywin32, but that isn't working now. I'm using pywin32 version 223.
Upvotes: 3
Views: 2604
Reputation: 189
Try the following script and see if it works:
wb = xl.Workbooks.Open(os.path.abspath(self.excel_parser_location), ReadOnly=1, CorruptLoad=1)
Upvotes: 0
Reputation: 471
Sometimes this error occured when path to file is too long. So you should try this code with file with shorter path
Upvotes: 0