Skip to content

childmindresearch/cmi-docx

CMI-docx

Build codecov Ruff stability-stable LGPL--2.1 License pages

cmi-docx is a Python library for manipulating .docx files built on top of python-docx. It extends the functionality of python-docx by providing additional features and utilities for working with .docx files.

Features

  • Find and replace.
  • Insert paragraphs in the middle of a document.
  • Manipulate styles and formatting of paragraphs' substrings.

Installation

Install this package from pypi using your favorite package manager. For example, using pip:

pip install cmi-docx

Quick start

The following example demonstratesa few features of cmi-docx:

import docx

from cmi_docx import document

doc = docx.Document()
paragraph = doc.add_paragraph("Hello, world!")
extend_document = document.ExtendDocument(doc)
extend_paragraph = document.ExtendParagraph(paragraph)

# Find and replace text.
extend_document.replace("Hello", "Hi", {"bold": True})

# Insert and image
extend_document.insert_image(index=1, image_path="path/to/image.png")

# Reformat a paragraph
extend_paragraph.format(italics=True)