Python: Batch Convert .docx to .pdf
About
This script will convert all the .docx files found within a folder to .pdf upon execution.
Raw Script
batch-convert-docx-to-pdf.py
from docx2pdf import convert
import os
import time
def convert_docx_to_pdf(folder_path):
for filename in os.listdir(folder_path):
if filename.endswith(".docx"):
docx_path = os.path.join(folder_path, filename)
pdf_path = os.path.splitext(docx_path)[0] + ".pdf"
convert(docx_path, pdf_path)
time.sleep(1) # Add a delay of 1 second between conversions
os.remove(docx_path) # Delete the original DOCX file
# Get the current directory where the script is located
script_directory = os.path.dirname(os.path.abspath(__file__))
# Use the script directory as the input folder for DOCX files
folder_path = script_directory
# Call the function to convert DOCX to PDF
convert_docx_to_pdf(folder_path)
Dependencies
pip install docx2pdf