Python | Converting Docx File to PDF File

Python can be used for converting Docx File to a PDF File using docx2pdf package which is developed by AlJohri. This package contains convert() function which takes in a Docx File as parameter and then parse it to a PDF File.
Let’s see step-by-step How Docs File can be converted to PDF File using Python.

  • Firstly install docx2pdf package using pip (pip install docx2pdf)
  • Import docx2txt package into Python Code file using import docx2pdf statement
  • Pass docx file to function docx2pdf.convert(parameter) as a parameter
  • Python will automatically save docx file’s equivalent PDF into same directory as docx File was in

Just note here pip install docx2pdf will install this package for Python2.7 on Mac Laptops. And if you run your program using Python3 then it will show ModuleNotFoundError: No module named ‘docx2pdf’ Error. To avoid this situation, install this package for Python3 version using python3 -m pip install docx2pdf, this will specifically install docx2pdf for latest Python3 version.

Also I would strongly recommend you to keep your python code file and docx files into same folder as then you doesn’t need to specify any directory inside code file.
• Test Folder
         • program.py
         • test.docx

When program.py is run docx2pdf.convert(parameter) function will save newly made PDFs under Test Folder.

Let’s now see Python Code for Converting Docx File(For example => test.docx) to PDF File(For example => test.pdf).

# Python3 program to convert docx to pdf using docx2pdf module 

# Import package docx2pdf
import docx2pdf
  
# Converting docx present in the same folder as the python file to PDF
docx2pdf.convert("test.docx") 

After running this program a file named test.pdf (Just replaced docx with pdf after file passed to convert function) will be created in Test Folder.
• Test Folder
         • program.py
         • test.docx
         • test.pdf

In the below on left hand side is test.docx and on right hand side is converted test.pdf file

Docx File to PDF File using Python

This is how a Docx File can be converted to PDF file using Python and docx2pdf package. Also check out source code of docx2pdf package on github, if your interested => Docx2pdf Python Package Source Code.

Also I’ve written an article about How to convert Docx File to Test File using Python, if your interested then you can check that article here => How to convert Docx File to Text File using Python and docx2txt package?

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts