簡體   English   中英

將 pdf 文件打印到打印機,Python 3.10.6

[英]print pdf file to printer, Python 3.10.6

我有一個 function 將生成的 .docx 文件轉換為 .PDF 並將其保存到目錄中,保存后我想將其打印到打印機。

我的文件名有問題,我無法讀取目錄。

pywintypes.error: (2, 'ShellExecute', 'The system cannot find the file specified.')

我正在使用win32api.ShellExecute()進行打印。

doc.save("./cr/generated_doc"+itemid1+".docx")
pythoncom.CoInitialize()
convert("./cr/generated_doc"+itemid1+".docx",
        "./cr/pdf/label"+itemid1+".pdf")

printername = "Microsoft Print to PDF"
fileName = str(r"./cr/pdf/label"+itemid1+".pdf")
win32api.ShellExecute(0, "printto", filename, f'"{printername}"', ".", 0)

os.remove("./cr/generated_doc"+itemid1+".docx")
os.remove(fullnm)

在 Windows 上,目錄在路徑中由\分隔,而不是/ 為了獨立於操作系統處理路徑,您必須使用 Python 助手 function, os.path.join 使用它,您的代碼不應遇到與路徑相關的錯誤:

doc_file_path = os.path.abspath(os.path.join("cr", "generated_doc"+itemid1+".docx"))
pdf_file_path = os.path.abspath(os.path.join("cr", "pdf", "label"+itemid1+".pdf"))
doc.save(doc_file_path)
pythoncom.CoInitialize()
convert(doc_file_path, pdf_file_path)

printername = "Microsoft Print to PDF"
win32api.ShellExecute(0, "printto", pdf_file_path, f'"{printername}"', ".", 0)

os.remove(doc_file_path)
os.remove(fullnm)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM