Skip to main content

Edit Word documents but keep original formatting

Project description

PyPI Downloads PyPI version

docxedit

Edit Word documents effortlessly without changing the original formatting.

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

This is useful if you want to automate document writing or editing and need to adhere to strict formatting rules — a common requirement in professional settings.

Install

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.

  • Replace all occurrences of a string with a new string (optionally limit up to a paragraph number, and include or exclude tables)
  • Remove a paragraph or a line (and optionally a number of lines after it)
  • Add text to a table cell
  • Change the font size of a table
  • Show the line where a specific string is found

How to Use

from docx import Document
import docxedit

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

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

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

# Show the line where 'Hello' is found
docxedit.show_line(document, current_text='Hello')

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

# Remove a specific paragraph
docxedit.remove_paragraph(document.paragraphs[0])

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

# Change font size of the first table to 12pt
docxedit.change_table_font_size(document.tables[0], font_size=12)

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

Logging

By default, docxedit does not produce any console output. To enable debug logging:

import logging
logging.basicConfig(level=logging.DEBUG)

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.2.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docxedit-1.2.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docxedit-1.2.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for docxedit-1.2.1.tar.gz
Algorithm Hash digest
SHA256 804f86ec9f14c4494576bb7877470e4128147e1b90c2c552a42d3fcd962a4b31
MD5 ee44e00e35a175686bf36101d52f8d69
BLAKE2b-256 0382ace4bba63e1bb5ce3fa832055c3dd2cf8d1b9e886c21fdf3a931c8d531df

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxedit-1.2.1.tar.gz:

Publisher: release-please.yml on henrihapponen/docxedit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: docxedit-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for docxedit-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 32fe054c89918bb5b7dbfef01c8cf71e29c3e11f8234cd0de171929e8a794750
MD5 3645086014ef9d77271e19451670fb84
BLAKE2b-256 9ba6302efc4e35454d7be679df283ead13d31892e50c550916f8dbcf7c90e394

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxedit-1.2.1-py3-none-any.whl:

Publisher: release-please.yml on henrihapponen/docxedit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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