Skip to main content

Replaces fields in docx (Microsoft Office Word) files

Project description

PyPI

Udpates the MAILMERGE fields in Office Open XML (docx) files. Can be used on any system without having to install Microsoft Office Word. Supports Python 3.7 and up. For the moment only the INCLUDEPICTURE fields are supported.

It supports local images, URLs and base64 image encoded strings. Also, it allows the resize of the image both in width and height.

The fields are replaced so afterwards they cannot be updated anymore.

This library uses the excellent python-docx library.

Also, it is better used after mailmerging the INCLUDEPICTURE fields using the docx-mailmerge2 library.

Installation

Installation with pip:

$ pip install docx-mergefields

Usage

Open the file.

from mergefields import MergeFieldsDocument
with MergeFieldsDocument("../documents/mailmerge_doc.docx") as doc:
  doc.transform_fields()
  doc.doc.save("../documents/merged_doc.docx")

Examples

From local file, original size:

{ INCLUDEPICTURE "./filename.jpg" }

From a URL, resize width to the given size, resize height to preserving the aspect ratio:

{ INCLUDEPICTURE "https://www.pngall.com/wp-content/uploads/8/Sample-Watermark-PNG-Image.png" \w 200 }

From a base64 URI, resize both width and height to the given value (aspect ratio is not preserved):

{ INCLUDEPICTURE "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAADCCAMAAAB6zFdcAAAAtFBMVEX///9QUU/yX1xKS0mKiopNTkxCQ0FGR0VpamjS0tJHSEa9vbyvr67yXFlSU1FDRELyWVaoqKjc3N3xU1D19fXDxMNWV1XxUU797Ovv7+/j4+P3pKP5vLv+8/O3t7b71NP1i4nzc3D4rqz1hYPzZ2WCgoGUlJN3eHf0e3lgYV/83t2enp76xsXMzcyQkY/1k5H6ysn2k5L3qqg3ODX2m5rzb21lZmT5t7Xr39/su7rmqana4+QStYMYAAAEt0lEQVR4nO3ab3eiOBQGcGkkWFoEBUVtpbXV+qdqZ9S2uzvf/3stuQkQQNvdOT2j1Of3Yk9NMJhLckkyW6sBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICwWN7eLhfDA7VBuxNF0ar/R3/Sl3ObzaZ9daDy7t4Jfc/zQ2+23FPdmTL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kdOMwZYV8ZasefHyISguHIM6t5IjZca7st9PPG7BLNzDtBmzaDnxZpfudgIxmNSLJslTvCj8+8Ki+N0gmpqWy127u17pzRUTf0TNihzxVL7boeXmabjLzQZ/s/ei/mAwOHySUH07bTY4r59f/y2Ns9lQ2kGdi0U6G/z3Y/+Wo9n4aibMPr/22xo55z0ThB7NhvD22L/jqMQholc6WjwzM4fOEM9X0O//5Yd/9/vfeRn0kX5b+PXP4y/64wQ2+n9c0C6o+P+B93uC/iDp/2BwlhEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAID/61/qyEpXq0NhcQAAAABJRU5ErkJggg==" \w 200 \h 200 }

Todo / Wish List

  • Include other fields

  • Update the fields instead of replacing them

Contributing

  • Fork the repository on GitHub and start hacking

  • Create / fix the unit tests

  • Send a pull request with your changes

Unit tests

In order to make sure that the library performs the way it was designed, unit tests are used. When providing new features, or fixing bugs, there should be a unit test that demonstrates it. Run the test suite:

python -m unittest discover

Credits

This repository is written and maintained by Iulian Ciorăscu.

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

docx_mergefields-0.1.5.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

docx_mergefields-0.1.5-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file docx_mergefields-0.1.5.tar.gz.

File metadata

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

File hashes

Hashes for docx_mergefields-0.1.5.tar.gz
Algorithm Hash digest
SHA256 93ea6c9b9d6347fbdf39697ff3d402042cfc5f5df8f04ce39da6cd2cfb90ab3d
MD5 22a4023e51b21350461c2e6b402198ee
BLAKE2b-256 b685a1d7585bf5e7cd1e4f4f99acc8ef759d0765d0a09cc276fd60b7b934dc7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for docx_mergefields-0.1.5.tar.gz:

Publisher: python-publish.yml on iulica/docx-mergefields

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

File details

Details for the file docx_mergefields-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for docx_mergefields-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 28eca0cd02d69b3e166302289653175a9ab7bf4a00e5db7242c1a23a9611d735
MD5 f55d9e71065cfed1e2b06b55b28c3ab5
BLAKE2b-256 b7d050dc3d8e18d9f7066eeffec311aa7e3137e089c2266a87528ac5bc247fff

See more details on using hashes here.

Provenance

The following attestation bundles were made for docx_mergefields-0.1.5-py3-none-any.whl:

Publisher: python-publish.yml on iulica/docx-mergefields

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