Free Python API to Create & Edit Microsoft® Word Documents
Open Source Python Library for Creating, Editing, Reading and Managing MS Word Files, add Table & Images to Word DOCX Files & more inside Python Apps.
What is Python-DOCX?
Python-DOCX is an Open Source Python library that gives software developers the capability to work with Microsoft Word (Docx) inside their own applications. The API can create and modify Word documents, which have the .docx file extension. You can embed images, charts, and other media files into your documents.
The API is very productive and supports several important word processing features such as opening a document, adding a paragraph, adding a heading, adding a page break, adding a table, inserting images, applying a paragraph style, text formatting, and much more. The Library supports conditional formatting, allowing you to apply formatting rules based on specific conditions.
Getting Started with Python-DOCX
Python-DOCX is hosted on PyPI, so It is very simple to install it. It can be installed with pip using the following command.
Install Python-DOCX via pip command
pip install python-docx
It can also be installed via easy_install but is not recommended.
Python API to Create Word DOCX Documents
Python-DOCX library provides functionality for the creation as well as manipulation of Microsoft word DOCX documents. The API also enables software developers to modify word documents. Now, to open a word document, create an instance along with passing the path to the document. You also can add images, add headings, insert tables, font styles support, text formatting, and more. Using the following steps you can easily create a Microsoft Word document in your Python app easily.
Create Word Easily
- Initialize object of FastExcel
- Create workbook
- Populate row data
- Write Excel
How to Create Word Fastly - Python?
// initialize document object
document = Document()
// add heading
document.add_heading('Document Title', 0)
// add paragraph
p = document.add_paragraph('A plain paragraph having some ')
// style paragraph
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
// save word document
document.save('demo.docx')
Add a Table to Word DOCX Document
Python-DOCX API allows developers to add tables to a Word DOCX Document inside Python applications. There are several properties and methods linked to a table. In order to use the table, you will need to use them, for example accessing a table cell, table-border, accessing individual rows or columns, and more. Here is an example that shows how software developers can create a table inside word documents using Python API.
How to Create a Table inside Word Document using Python Library?
import docx
# Create a new document
document = docx.Document()
# Create a table with 2 rows and 3 columns
table = document.add_table(rows=2, cols=3)
# Add table cells
table.cell(0, 0).text = 'Cell 0,0'
table.cell(0, 1).text = 'Cell 0,1'
table.cell(0, 2).text = 'Cell 0,2'
table.cell(1, 0).text = 'Cell 1,0'
table.cell(1, 1).text = 'Cell 1,1'
table.cell(1, 2).text = 'Cell 1,2'
# Save the document
document.save('table_document.docx')
Add Images to Word DOCX Files
Python-DOCX provides Software programmers the ability to insert images inside their Word DOCX document. To add an image you need to provide the name and location of the image. By default, the added image appears at the native size. You can specify both the width and height of the image. The Inches and Cm classes are provided to let you specify measurements in convenient units.