Convert Figma logo to code with AI

python-openxml logopython-docx

Create and modify Word documents with Python

4,491
1,103
4,491
646

Top Related Projects

Use a docx as a jinja2 template

Quick Overview

The python-docx library is a Python library that provides a simple and intuitive interface for creating, reading, and modifying Microsoft Word (DOCX) documents. It allows developers to programmatically generate, manipulate, and extract data from DOCX files without the need for Microsoft Office or other third-party software.

Pros

  • Cross-platform Compatibility: The library is platform-independent and can be used on Windows, macOS, and Linux.
  • Easy to Use: The library has a straightforward and well-documented API, making it easy for developers to get started and integrate it into their projects.
  • Comprehensive Functionality: The library supports a wide range of DOCX features, including paragraphs, headings, tables, images, and more.
  • Open-source and Active Development: The project is open-source and actively maintained, with regular updates and bug fixes.

Cons

  • Limited Formatting Options: While the library supports a wide range of DOCX features, the level of control over formatting and layout may be limited compared to using Microsoft Word directly.
  • Performance Overhead: Generating or modifying large DOCX documents may have a noticeable performance impact, especially on older or less powerful hardware.
  • Dependency on lxml: The library relies on the lxml library, which can be challenging to install on some platforms, particularly Windows.
  • Lack of Advanced Features: The library may not provide the most advanced or specialized features for working with DOCX documents, such as complex document automation or integration with other Microsoft Office products.

Code Examples

Here are a few examples of how to use the python-docx library:

  1. Creating a new DOCX document:
from docx import Document
from docx.shared import Inches

document = Document()
document.add_heading('Document Title', 0)
document.add_paragraph('This is a sample paragraph.')
document.add_picture('image.png', width=Inches(2))
document.save('example.docx')
  1. Extracting text from a DOCX document:
from docx import Document

document = Document('example.docx')
for paragraph in document.paragraphs:
    print(paragraph.text)
  1. Modifying an existing DOCX document:
from docx import Document
from docx.shared import Inches

document = Document('example.docx')
document.paragraphs[0].text = 'New paragraph text'
table = document.add_table(rows=1, cols=2)
table.cell(0, 0).text = 'Name'
table.cell(0, 1).text = 'Age'
document.save('modified_example.docx')
  1. Inserting an image into a DOCX document:
from docx import Document
from docx.shared import Inches

document = Document()
document.add_heading('Document with Image', 0)
document.add_paragraph('This document contains an image.')
document.add_picture('image.png', width=Inches(2))
document.save('example_with_image.docx')

Getting Started

To get started with the python-docx library, follow these steps:

  1. Install the library using pip:
pip install python-docx
  1. Import the necessary modules from the library:
from docx import Document
from docx.shared import Inches
  1. Create a new DOCX document or open an existing one:
document = Document()
# or
document = Document('existing_document.docx')
  1. Add content to the document, such as paragraphs, headings, tables, and images:
document.add_heading('Document Title', 0)
document.add_paragraph('This is a sample paragraph.')
table = document.add_table(rows=1, cols=2)
table.cell(0, 0).text = 'Name'
table.cell(0, 1).text = 'Age'
document.add_picture('image.png', width=Inches(2))
  1. Save the document:
document.save('

Competitor Comparisons

Use a docx as a jinja2 template

Pros of python-docx-template

  • Simpler templating system for document generation
  • Supports conditional statements and loops in templates
  • Easier to work with existing Word documents as templates

Cons of python-docx-template

  • Less fine-grained control over document structure
  • Limited ability to create complex documents from scratch
  • Smaller community and fewer resources compared to python-docx

Code Comparison

python-docx:

from docx import Document

document = Document()
document.add_heading('Document Title', 0)
document.add_paragraph('A plain paragraph having some bold and some italic.')
document.save('document.docx')

python-docx-template:

from docxtpl import DocxTemplate

doc = DocxTemplate("my_template.docx")
context = { 'company_name': "World Company" }
doc.render(context)
doc.save("generated_doc.docx")

The python-docx library provides more programmatic control over document creation, while python-docx-template focuses on filling pre-designed templates with data. python-docx is better suited for creating documents from scratch, while python-docx-template excels at generating documents based on existing templates with placeholders.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

python-docx

python-docx is a Python library for reading, creating, and updating Microsoft Word 2007+ (.docx) files.

Installation

pip install python-docx

Example

>>> from docx import Document

>>> document = Document()
>>> document.add_paragraph("It was a dark and stormy night.")
<docx.text.paragraph.Paragraph object at 0x10f19e760>
>>> document.save("dark-and-stormy.docx")

>>> document = Document("dark-and-stormy.docx")
>>> document.paragraphs[0].text
'It was a dark and stormy night.'

More information is available in the python-docx documentation