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:
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: