Thursday, December 26, 2024
Google search engine
HomeLanguagesConvert Docx to Pdf using docx2pdf Module in Python

Convert Docx to Pdf using docx2pdf Module in Python

Tired of having to use online docx to PDF converters with crappy interfaces and conversion limits? Then, look no further than your friendly neighborhood language python’s docx2pdf module. This module is a hidden gem among the many modules for the python language.

This module can be used to convert files singly or in bulk using the command line or a python program.

Installation

This module does not come built-in with Python. To install this module type the below command in the terminal.

pip install docx2pdf

Conversion using the command line

The basic structure of the docx2pdf command line usage is:

docx2pdf [input] [output]

If only the input file is specified, it generates a pdf from the docx and stores it in the same folder.

Example:

docx2pdf usage using the command line

Lazyroar folder containing both the original GFG.docx and the converted GFG.pdf

Original GFG.docx on the left and GFG.pdf on the right

For the bulk conversion, you can specify the folder containing all the Docx files. The converted pdfs will get stored in the same folder.

docx2pdf GeeksForGeeks_Folder/

You can also explicitly specify the input and output file or folder by specifying the path.

Conversion by importing the module and using it in the program

An endless number of useful applications can be made using this module.

Python3




# Python3 program to convert docx to pdf
# using docx2pdf module
 
# Import the convert method from the
# docx2pdf module
from docx2pdf import convert
 
# Converting docx present in the same folder
# as the python file
convert("GFG.docx")
 
# Converting docx specifying both the input
# and output paths
convert("GeeksForGeeks\GFG_1.docx", "Other_Folder\Mine.pdf")
 
# Notice that the output filename need not be
# the same as the docx
 
# Bulk Conversion
convert("GeeksForGeeks\")


Output:

RELATED ARTICLES

Most Popular

Recent Comments