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

Uploaded Source

Built Distribution

docx_mergefields-0.1.3-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: docx_mergefields-0.1.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for docx_mergefields-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2df2e352477fc33edbc4c82adf13775225b915b1e556a7e5625535665897fd10
MD5 852afe3440667b3dcff7ced8732b6952
BLAKE2b-256 ec597833d4123853603972a5d25277840c1b7150bb5c0fafcfab50bbb8e4238a

See more details on using hashes here.

File details

Details for the file docx_mergefields-0.1.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for docx_mergefields-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5185a9a166abb1d720cc66e6a7ed26129720c2015d5d946aab59b347ba645f36
MD5 48341295777896e00baa9a3aa3c617e8
BLAKE2b-256 349c1d991a1acb363c22000ede41d4bd2c355c91d771dad7b49504a7df86473c

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