Word to PDF is one of the most popular and immensely performed document conversions. The DOCX or DOC files are converted to PDF format before they are printed or shared. In this article, we will automate Word to PDF conversion in Python. The steps and code samples will demonstrate how to convert DOCX or DOC files to PDF within a few lines of Python code. Also, you will learn about different options to customize Word to PDF conversion.
- Python API for DOCX/DOC to PDF Conversion
- Convert Word DOCX/DOC to PDF
- Word to PDF with a Particular PDF Standard
- Convert Range of Pages in Word document to PDF
- Apply Image Compression in Word to PDF
Python API for Word to PDF Conversion
For converting Word documents to PDF format, we will use Aspose.Words for Python. It is a feature-rich Python library for creating and manipulating Word documents. Moreover, it lets you convert DOCX and DOC files to PDF format with high fidelity. The library is hosted on PyPI and you can install it using the following pip command.
pip install aspose-words
Convert Word DOCX/DOC to PDF using Python
The following are the steps to convert a Word document to PDF in Python.
- Load the Word document using Document class.
- Convert Word document to PDF using Document.save() method.
The following code sample shows how to convert a Word DOCX file to PDF.
Python Word to PDF with a Particular Standard
You can also specify the particular standard for the converted PDF document such as PDF/A. The following are the steps to specify the PDF standard in Word to PDF conversion using Python.
- Load the Word document using Document class.
- Create an object of PdfSaveOptions class and set PDF standard using PdfSaveOptions.compliance property.
- Convert Word document to PDF using Document.save() method.
The following code sample shows how to set a particular standard in Word to PDF conversion.
Convert Range of Pages in DOCX/DOC to PDF using Python
You can also specify the range of pages you want to convert to PDF format. For this, you can use PdfSaveOptions.page_set property. The following code sample shows how to convert a range of pages in Word document to PDF.
Apply Image Compression in DOCX to PDF using Python
Aspose.Words for Python also lets you apply image compression in the converted PDF document. In addition, you can specify the JPEG quality for the images. The following are the steps to set image compression while converting a Word document to PDF in Python.
- Load the Word document using Document class.
- Create an object of PdfSaveOptions class.
- Set image compression using PdfSaveOptions.image_compression property.
- Set JPEG quality using PdfSaveOptions.jpeg_quality property.
- Convert Word document to PDF using Document.save() method.
The following code sample shows how to set image compression in Word to PDF conversion.
Get a Free API License
You can get a temporary license in order to use Aspose.Words for Python without evaluation limitations.
Info: You may be interested in another Python API that allows developers and applications to convert PowerPoint to PDF – Aspose.Slides for Python. And you might also want to check out Aspose PowerPoint to PDF Converter because it is a live implementation of the PowerPoint to PDF conversion process.
Conclusion
In this article, you have learned how to convert Word DOCX or DOC files to PDF in Python. Moreover, you have seen different options to customize the Word to PDF conversion programmatically. You can learn more about Aspose.Words for Python using documentation. In case you would have any questions, feel free to let us know via our forum.