批量将excel另存为pdf,在某一份文件时报错,不知道是什么原因,其他excel文件都没有问题,拿出来单独运行代码依然报错
报错信息
Traceback (most recent call last):
File "C:\Users\gz\Desktop\eb\worf\main.py", line 324, in run
self.openexcel()
File "C:\Users\gz\Desktop\eb\worf\main.py", line 277, in openexcel
self.exceltopdf(i)
File "C:\Users\gztsrayz\Desktop\search2web\word-pdf\main.py", line 290, in exceltopdf
ws.ExportAsFixedFormat(0, self.name[i][:-4]) #不需带文件后缀
File "<COMObject <unknown>>", line 6, in ExportAsFixedFormat
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147024809), None)
#数据来源(遍历得到文件位置,并且其他文件均无问题,所以路径不应有问题)
for i in os.listdir(self.path_excels):
if i.split('.')[-1] in ['xlsx','xls']:
this_excel = os.path.join(self.path_excels,i)
self.excels.append(this_excel)
def openexcel(self):
self.excel = win32com.client.DispatchEx('Excel.Application')
self.excel.Visible = False # 是否可视化
self.excel.DisplayAlerts = False
for i in range(len(self.excels)):
self.exceltopdf(i)
self.close_excel_by_force()
def exceltopdf(self,i):
wb = self.excel.Workbooks.Open(self.excels[i], ReadOnly=False)
ws = wb.Worksheets(1)
# target_name=os.path.join(self.pdf_small,self.name[i])
ws.PageSetup.Zoom = False
ws.PageSetup.FitToPagesTall = 1
ws.PageSetup.FitToPagesWide = 1
print(self.excels[i],self.name[i])
ws.ExportAsFixedFormat(0, self.name[i][:-4]) #不需带文件后缀
wb.Close()