From 92f32202f1bd500dd2a7dfdf54629689b46b96dc Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Tue, 3 Jun 2025 20:42:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/worker.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/module/worker.py b/module/worker.py index cf092fc..6afc531 100644 --- a/module/worker.py +++ b/module/worker.py @@ -1,6 +1,7 @@ import os import traceback +import pythoncom from PySide6.QtCore import QObject, Signal from win32com import client @@ -52,11 +53,14 @@ class DTGWorker(QObject): if os.path.exists(pdf_file): os.remove(pdf_file) - word = client.Dispatch("Word.Application") - doc = word.Documents.Open(word_file) - doc.SaveAs(pdf_file, 17) - doc.Close() - word.Quit() + # https://stackoverflow.com/questions/71292585/python-docx2pdf-attributeerror-open-saveas + word = client.Dispatch("Word.Application", pythoncom.CoInitialize()) + try: + doc = word.Documents.Open(word_file) + doc.SaveAs(pdf_file, 17) + doc.Close() + finally: + word.Quit() os.remove(word_file) os.startfile(pdf_file)