33import subprocess
44import platform
55import threading
6+ import traceback
7+ import win32com .client
68from tkinter import Tk , Label , Button , filedialog , messagebox , StringVar , Frame
79from docx2pdf import convert
810from PyPDF2 import PdfMerger
911
1012dot_count = 0
1113running = False
1214
15+ def unblock_file (filepath ):
16+ if os .name == 'nt' :
17+ try :
18+ zone_file = filepath + ":Zone.Identifier"
19+ if os .path .exists (zone_file ):
20+ os .remove (zone_file )
21+ except Exception as e :
22+ print (f"Failed to unblock { filepath } : { e } " )
23+
24+
1325def run_conversion (input_folder , output_folder ):
1426 global running
1527 status_percent .config (text = f"0%" )
@@ -30,13 +42,25 @@ def run_conversion(input_folder, output_folder):
3042 for i , file in enumerate (word_files , start = 1 ):
3143 if file .lower ().endswith (".docx" ):
3244
33- original_src = os .path .join (input_folder , file )
45+ unblock_file (os .path .join (input_folder , file ))
46+
47+ original_src = os .path .abspath (os .path .join (input_folder , file ))
3448 temp_dst = os .path .join (temp_word_dir , file )
3549 shutil .copy2 (original_src , temp_dst )
3650
3751 pdf_path = os .path .join (temp_dir , file .replace (".docx" , ".pdf" ))
3852 try :
39- convert (temp_dst , pdf_path )
53+ word_app = win32com .client .Dispatch ("Word.Application" )
54+ print ("opening" )
55+ doc = word_app .Documents .Open (original_src )
56+ print ('opened' )
57+ try :
58+ doc .ExportAsFixedFormat (pdf_path , 17 )
59+ except Exception as e :
60+ print (f"Word export failed for { file } " )
61+ print (e )
62+ traceback .print_exc ()
63+ raise
4064 except Exception as e :
4165 messagebox .showerror ("Error" , f"Failed to convert { file } : { e } " )
4266 continue
0 commit comments