python - Pywin32 COM objects passed to function cause pywintypes.com_error -
update: see bottom of post
i'm having small issue function designed create new workbook in excel, copy of data passed in sheet separate workbook, , save workbook.
so, have function coded below , reason line:
sheet.range(sheet.cells(1, 1), sheet.cells(row_count, max_col)).copy()
throws error:
traceback (most recent call last): file "reports-script.py", line 191, in <module> format(workbook_filename, query_list, data, colu mn_orderings) file "reports-script.py", line 168, in format excel.save_all_in_workbook_as_mht() file ".....\excel.py", line 381, in save_all _as_mht filename = self.save_copy_as_mht(sheet, sheet.name, self.folde r_name) file ".....\excel_class.py", line 359, in save_cop y_as_mht self.excel.workbooks(1).sheets(sheet.name).cells(row_count, max_col) file ".....\win32com\client\dynamic.py", line 172, in __call__ return self._get_good_object_(self._oleobj_.invoke(*allargs),self._olerepr_. defaultdispatchname,none) pywintypes.com_error: (-2147352567, 'exception occurred.', (0, none, none, none, 0, -2147352571), 2)
i printed passed in sheet object, , showed as:
<comobject <unknown>>
but able retrieve name of sheet accessing name attribute
sheet.name
so, i'm not sure if part of issue i'm seeing or not.
here full function body.
def save_copy_as_mht(self, sheet, the_filename, max_col): self.excel.workbooks.add() num_workbooks = self.excel.workbooks.count #count occupied rows row_count = self.__count_used_rows(sheet, cutoff_number = -1) #copy , paste rows new sheet sheet.range(sheet.cells(1, 1), sheet.cells(row_count, max_col)).copy() self.excel.workbooks(num_workbooks).sheets(1).pastespecial() self.excel.workbooks(num_workbooks).sheets(1).pastespecial(xl_paste_column_widths) #saves new worksheet using query name, overwrites existing copies self.save_workbook_as_mht(self.excel.workbooks(num_workbooks), the_filename, self.folder_name) #close workbook self.excel.workbooks(num_workbooks).close()
i realize there 2 function calls above code have not posted, execution not reach far in function 1 of them, , other 1 returns integer.
thanks!
update: alright appear if replace variable references "row_count" , "max_col" hard-coded integers works fine.....but doesn't solve problem diagnoses more clearly. why com function calls fail when passed python variables? , how can fix this??
Comments
Post a Comment