Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created April 24, 2024 07:00
Show Gist options
  • Save me-suzy/80bb46b3a4a7d0d5972d3e2d347860c6 to your computer and use it in GitHub Desktop.
Save me-suzy/80bb46b3a4a7d0d5972d3e2d347860c6 to your computer and use it in GitHub Desktop.
Python: Convert docx to PDF
import os
from pathlib import Path
from docx2pdf import convert
# The location where the files are located
input_path = r'c:\Folder7\input'
# The location where we will write the PDF files
output_path = r'c:\Folder7\output'
# Create the output directory if it doesn't exist
os.makedirs(output_path, exist_ok=True)
# Check if the input directory exists
directory_path = Path(input_path)
if not directory_path.exists() or not directory_path.is_dir():
print(directory_path, "is invalid")
sys.exit(1)
# Convert each .docx file to .pdf
for file_path in directory_path.glob("*.docx"):
print("Converting file:", file_path)
output_file_path = os.path.join(output_path, file_path.stem + ".pdf")
convert(file_path, output_file_path)
print("Converted file:", file_path, "to", output_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment