Skip to main content

A fast Python library for merging two Microsoft Word (.docx) documents into one.

Project description

📄 docx-merge-xml

A fast Python library for merging two Microsoft Word (.docx) documents into one. Easily insert content at specific positions or based on placeholder patterns.


📦 Installation

Install via pip:

pip install docx-merge-xml

📚 Dependencies

Only one dependency:

  • lxml – for parsing and modifying the DOCX XML content

🛠️ API

merge_docx(
	source_path: str,
	content_path: str,
	output_path: str | None = None,
	pattern: str | None = None,
	insert_start: bool = False,
	insert_end: bool = False,
) -> BytesIO

Parameters:

  • source_path (required) – Path to the base .docx file
  • content_path (required) – Path to the .docx file to insert into the base
  • output_path – Path to save the merged document
  • pattern – Placeholder string in the source file to be replaced with inserted content
  • insert_start – Insert the content at the beginning of the source file
  • insert_end – Insert the content at the end of the source file

🔔 Note: You can combine pattern, insert_start, and insert_end and at least one is required.

💡 Examples

Replace a placeholder in the source DOCX

from docx_merge import merge_docx

buffer = merge_docx(
	source_path="./source.docx",
	content_path="./table.docx",
	pattern="{{table}}"
)

# Use buffer (e.g., send as a response in a server)

Save the output docx by providing an output_path

from docx_merge import merge_docx

buffer = merge_docx(
	source_path="./source.docx",
	content_path="./table.docx",
	output_path="./output.docx",
	pattern="{{table}}"
)

Insert at the start of the document

from docx_merge import merge_docx

merge_docx(
	source_path="./source.docx",
	content_path="./table.docx",
	output_path="./output.docx",
  insert_start=True
)

Insert at the end of the document

from docx_merge import merge_docx

merge_docx(
	source_path="./source.docx",
	content_path="./table.docx",
	output_path="./output.docx",
  insert_end=True
)

🧪 Testing

This project uses Pytest for unit testing.

To run tests:

poetry run pytest

🔒 License

MIT

🤝 Contributing

Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.

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_merge_xml-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

docx_merge_xml-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file docx_merge_xml-0.1.1.tar.gz.

File metadata

  • Download URL: docx_merge_xml-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.11.0-1013-azure

File hashes

Hashes for docx_merge_xml-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8c35e7eb3c1c69a1c1c373ce894402e28a40d31e677121b1ed3bef102a2e6981
MD5 a50d54e5b657b9450e3c2268110d6d34
BLAKE2b-256 1c374e22e71b177446788a28aebe5994f8fd035085a6b8be452ad75c51d06b11

See more details on using hashes here.

File details

Details for the file docx_merge_xml-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: docx_merge_xml-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.11.0-1013-azure

File hashes

Hashes for docx_merge_xml-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 847da13ea5c00eb6157bcae48ea20b3e38d032ec20d221a5e930484b1ebb0661
MD5 b759f1d120e3423d08366b2f7917f9ea
BLAKE2b-256 24f4355079217dc4cb56f41e7be8263c784a9ba5dafc95b03c50b60d67c6b376

See more details on using hashes here.

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