excelをpdf化したい。
下記エラーのためexcelをpdf化できません。
ご存じの方がおられましたら解決方法を教えいただけませんでしょうか。
出現エラー内容
****** Exception ******
Traceback (most recent call last):
File "d:\Programming\Visual_Studio_Code\kbk_erp\mitsumori\print.py", line 190, in print_excel
file.ActiveSheet.ExportAsFixedFormat(0, pdf_path)
File "<COMObject <unknown>>", line 3, in ExportAsFixedFormat
pywintypes.com_error: (-2147352567, '例外が発生しました。', (0, None, None, None, 0, -2147024809), None)
プログラムコード
pdf_path = "C:\temp\a"
file_path_target = 'C:\temp\見積_印刷.xlsx'
pdf_path = 'C:\temp\a.pdf'
try: #ここからpdf化 excel = win32com.client.Dispatch("Excel.Application") # エクセルを展開 print(f"{file_path_target} -> open file...") file = excel.Workbooks.Open(file_path_target) # 対象シートを指定 file.WorkSheets("フォーマット_1ページ用").Activate() print(f"{file_path_target} -> exporting...") #PDFに変換 file.ActiveSheet.ExportAsFixedFormat(0, pdf_path) print(f"{pdf_path} -> completed!") file.close() except: file.close()
