Convert PDF to Word DOCX File via Free Python API
Leading Open Source Python Library for Creating & Manipulating Microsoft Word DOCX Documents. Dynamically Create DOCX Files, Embed Text, Tables, Images, & more inside Python Apps.
What is Py2Docx?
Open source libraries have become indispensable for developers seeking to streamline their workflows and build robust applications without reinventing the wheel. The world of document creation has evolved significantly, with DOCX emerging as a prominent file format. But what if you need to programmatically generate DOCX documents within your Python applications? Enter Py2Docx, a powerful open source library that empowers developers to craft professional-looking DOCX files with ease. The library makes it simple to add and style text in Word documents. It supports Custom fonts, sizes, and colors, text alignment options (left, center, right, justified) and Paragraph styling for spacing, indentation, and line breaks. It enables developers to automate document generation, eliminating repetitive tasks which is very useful in tasks like invoice generation, report creation, or certificate production.
Py2Docx is an open-source Python library designed to simplify the generation and manipulation of Microsoft Word documents (.docx). Developed by Rafael S., the library provides a user-friendly interface for dynamically creating Word documents, embedding text, tables, images, add sections and more. It is hosted on GitHub, making it accessible to the global developer community. The library’s support for custom styles and templates allows developers to create documents that match branding guidelines or user-specific requirements. As a Python library, Py2Docx seamlessly integrates with other Python frameworks and tools, such as Django or Flask. By incorporating the library, developers can save time, reduce costs, and deliver high-quality, customized documents to users.
Getting Started with Py2Docx
Py2Docx is hosted on PyPI, so It is very simple to install it. It can be installed with pip using the following command.
Install Py2Docx via pip command
$ pip install py2docx
Word DOCX File Creation via Python
The open source Py2Docx library has provided complete support for creating new DOCX documents inside Python applications. The library makes it simple to add text, insert images, add tables and style text in Word documents with ease. Here is a simple code example that shows how to creates a new DOCX document with a paragraph containing the specified text. The style property is used to set the paragraph style (optional).
How to Create New Microsoft Word DOCX Document inside Python Apps?
from docx import Document
document = Document()
# Add a paragraph with formatted text
paragraph = document.add_paragraph("This is a paragraph generated using Py2Docx.")
paragraph.style = 'Normal' # Optional: Set the paragraph style
# Save the document
document.save('my_document.docx')
Create & Manage Tables in DOCX via Python
Creating tables in Word documents is a common requirement, and open source Py2Docx library simplifies this process with robust table creation and manipulation features. The library supports adding tables with custom row and column counts, populate cells with text or nested elements, style tables with borders, shading, alignment options and so on. The following code snippet demonstrates creating and populating a table with headers and data inside Python applications.
How to Create & Populate Table with Headers and Data via Python API?
# Create a table with 2 rows and 3 columns
table = doc.add_table(rows=2, cols=3)
# Populate table cells
table.cell(0, 0).text = 'Header 1'
table.cell(0, 1).text = 'Header 2'
table.cell(0, 2).text = 'Header 3'
table.cell(1, 0).text = 'Data 1'
table.cell(1, 1).text = 'Data 2'
table.cell(1, 2).text = 'Data 3'
# Save the document
doc.save('table_example.docx')
Incorporate Images into Word DOCX via Python
With Py2Docx, software developers can embed images into word DOCX documents effortlessly with just a couple of lines of Python code. The library support custom sizing and alignment. The following code example embeds an image in the Microsoft Word document, scales it to the specified dimensions, and centers it on the page using Python library.
How to Embed an Image in Word Document via Python Library?
# Add an image with custom size and alignment
doc.add_image('example.png', width=200, height=100, alignment='center')
# Save the document
doc.save('image_embedding.docx')