Skip to main content

Edit Word documents but keep original formatting

Project description

Mass Edit Word Documents Without Changing the Original Formatting

Edit Word documents effortlessly and without changing the original formatting of the document.

The original docx library is great but it's missing one important feature: keeping the original formatting.

However, this functionality is pretty useful if you want to automate document writing or editing and need to adhere to strict formatting rules. This is a common requirement in corporate environments for example.

This module adds that feature with some functions that allow editing documents without changing the original formatting.

PyPI version

Install

With pip: pip install docxedit

Dependencies

Included as a dependency: python-docx (docx)

Functionalities

Most of the functions in this module work primarily with runs, which are sequences of strings with the same formatting style. Breaking the document into runs allows us to edit the text without changing the original formatting.

Some of the functionalities that this module include:

  • Replacing all occurrences of a string with a new string (optionally limit this up to a paragraph number, and include or exclude tables)
  • Removing a line that includes a specific string
  • Add text to a table

The beauty of this module is that you can use all of its functions to mass edit Word documents with consistency and precision. This is useful especially in corporate environments where a lot of document writing or editing can be automated.

How to Use

Usage of this module is really simple. Here are some examples:

from docx import Document
import docxedit

document = Document('path/to/your/document.docx')

# Replace all instances of the word 'Hello' in the document with 'Goodbye' (including tables)
docxedit.replace_string(document, old_string='Hello', new_string='Goodbye')

# Replace all instances of the word 'Hello' in the document with 'Goodbye' but only
# up to paragraph 10
docxedit.replace_string_up_to_paragraph(document, old_string='Hello', new_string='Goodbye', 
                                        paragraph_number=10)

# Remove any line that contains the word 'Hello' along with the next 5 lines after that
docxedit.remove_lines(document, first_line='Hello', number_of_lines=5)

# Add text in a table cell (row 1, column 1) in the first table in the document
docxedit.add_text_in_table(document.tables[0], row_num=1, column_num=1, new_string='Hello')

# Save the document
document.save('path/to/your/edited/document.docx')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docxedit-1.0.4.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

docxedit-1.0.4-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file docxedit-1.0.4.tar.gz.

File metadata

  • Download URL: docxedit-1.0.4.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for docxedit-1.0.4.tar.gz
Algorithm Hash digest
SHA256 37def541044495a7a1f232fbf2e10e4befd3d03baab1b0178abe3066b7d58c36
MD5 db7dcaed1d067963c225a37b4b388e22
BLAKE2b-256 d88dd21146615ba13cd5cd764b2f87063eb738a68a9980e6d386cf46781cef3c

See more details on using hashes here.

File details

Details for the file docxedit-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: docxedit-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for docxedit-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e98383e27c5f19b0fd77b9251f3856d07808a01ceec7338ec255a03ac4d85d75
MD5 3fc77dfe5de2209fb67971922ca667a7
BLAKE2b-256 739290f07834bffff592d43adc205bb9d0438ef78876a8c7471dd70c19efcb8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page