Advertisement
nicuf

Convert .doc to .docx

Sep 7th, 2023
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import os
  2. import sys
  3. from pathlib import Path
  4. import win32com.client
  5. from docx2pdf import convert
  6.  
  7. # The location where the files are located
  8. input_path = r'c:\Folder7\input'
  9. # The location where we will write the PDF files
  10. output_path = r'c:\Folder7\output'
  11.  
  12. # Create the output directory if it doesn't exist
  13. os.makedirs(output_path, exist_ok=True)
  14.  
  15. # Check if the input directory exists
  16. directory_path = Path(input_path)
  17. if not directory_path.exists() or not directory_path.is_dir():
  18.     print(directory_path, "is invalid")
  19.     sys.exit(1)
  20.  
  21. # Convert .doc files to .docx
  22. word = win32com.client.Dispatch("Word.Application")
  23. for file_path in directory_path.glob("*.doc"):
  24.     docx_file_path = os.path.join(output_path, file_path.stem + ".docx")
  25.     doc = word.Documents.Open(str(file_path))
  26.     doc.SaveAs(docx_file_path, FileFormat=16)  # FileFormat 16 is for .docx
  27.     doc.Close()
  28. word.Quit()
  29.  
  30. # Convert each .docx file to .pdf
  31. for file_path in directory_path.glob("*.docx"):
  32.     print("Converting file:", file_path)
  33.     output_file_path = os.path.join(output_path, file_path.stem + ".pdf")
  34.     convert(file_path, output_file_path)
  35.     print("Converted file:", file_path, "to", output_file_path)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement