Advertisement
nicuf

Python: Convert docx to PDF

Apr 24th, 2024
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import os
  2. from pathlib import Path
  3. from docx2pdf import convert
  4.  
  5. # The location where the files are located
  6. input_path = r'c:\Folder7\input'
  7. # The location where we will write the PDF files
  8. output_path = r'c:\Folder7\output'
  9.  
  10. # Create the output directory if it doesn't exist
  11. os.makedirs(output_path, exist_ok=True)
  12.  
  13. # Check if the input directory exists
  14. directory_path = Path(input_path)
  15. if not directory_path.exists() or not directory_path.is_dir():
  16.     print(directory_path, "is invalid")
  17.     sys.exit(1)
  18.  
  19. # Convert each .docx file to .pdf
  20. for file_path in directory_path.glob("*.docx"):
  21.     print("Converting file:", file_path)
  22.     output_file_path = os.path.join(output_path, file_path.stem + ".pdf")
  23.     convert(file_path, output_file_path)
  24.     print("Converted file:", file_path, "to", output_file_path)
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement