Created
April 16, 2025 17:20
-
-
Save idcesares/5297d3771c030a817e8ae99fecef87fd to your computer and use it in GitHub Desktop.
Converts all docx files in a folder to pdf using LibreOffice CLI
This file contains hidden or 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 | |
import subprocess | |
import platform | |
def convert_docx_to_pdf(input_folder, output_folder): | |
"""Converts all docx files in the input folder to pdf and saves them in the output folder. | |
Args: | |
input_folder: The path to the folder containing the docx files. | |
output_folder: The path to the folder where the converted pdf files will be saved. | |
""" | |
# Create the output folder if it doesn't exist. | |
os.makedirs(output_folder, exist_ok=True) | |
# Loop through each file in the input folder. | |
for filename in os.listdir(input_folder): | |
# Check if the file is a docx file. | |
if filename.endswith(".docx"): | |
# Construct the full path to the input file. | |
input_path = os.path.join(input_folder, filename) | |
# Construct the full path to the output file. | |
output_path = os.path.join(output_folder, filename[:-5] + ".pdf") # Replace .docx with .pdf | |
# Use LibreOffice to convert the docx file to pdf. | |
subprocess.run([ | |
'libreoffice', | |
'--headless', # Run LibreOffice in headless mode (no GUI). | |
'--convert-to', 'pdf', # Specify the output format as pdf. | |
'--outdir', output_folder, # Specify the output folder. | |
input_path # Specify the input file path. | |
]) | |
if __name__ == "__main__": | |
# Reminder to install LibreOffice | |
print("Remember to install LibreOffice if you haven't already.") | |
print("Here's how to install it on different operating systems:") | |
print(" - Windows/macOS: Download and install from the official website: ") | |
print(" - Linux (Debian/Ubuntu): Run 'sudo apt-get install libreoffice'") | |
print(" - Linux (Fedora): Run 'sudo dnf install libreoffice'") | |
print(" - Other Linux distributions: Refer to your distribution's package manager.") | |
print("Make sure LibreOffice is added to your system's PATH environment variable.") | |
# Define the input and output folders. | |
input_folder = "docx_files" # Folder containing docx files. | |
output_folder = "pdf_files" # Folder to store converted pdf files. | |
# Call the function to convert the files. | |
convert_docx_to_pdf(input_folder, output folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment