-
-
Save me-suzy/80bb46b3a4a7d0d5972d3e2d347860c6 to your computer and use it in GitHub Desktop.
Python: Convert docx to PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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