Skip to main content

Library for reading and editing properties in Word documents

Project description

docx_properties

docx_properties is a Python library designed for reading, editing, and managing custom and core metadata properties in Microsoft Word .docx documents. It allows developers to programmatically interact with properties such as title, subject, author, and custom-defined metadata fields.

Features

  • Custom Properties:
    • Read, update, and filter custom properties in Word documents.
    • Add new custom properties or modify existing ones.
  • Core Properties:
    • Access core document properties like title, subject, creator, and revision number.
  • Field Updates:
    • Automatically update fields in Word documents (Windows only, requires pywin32).
  • Command-Line Interface (CLI):
    • Quickly manage document properties without writing Python code.

Installation

Install the library using pip:

pip install docx_properties

For Windows users who require field updating functionality:

pip install pywin32

Usage

Python API

The library provides an easy-to-use API for interacting with Word document properties.

Example: Setting a Custom Property

You can use the set_custom_property method to add or update custom properties in a Word document.

from docx_properties import DocxProperties

# Open the Word document
doc = DocxProperties("example.docx")

# Set or update a custom property
property_name = "ApprovalStatus"
property_value = "Approved"

# Add the custom property to the document
success = doc.set_custom_property(property_name, property_value)

if success:
    print(f"The custom property '{property_name}' was successfully set to '{property_value}'.")
else:
    print(f"Failed to set the custom property '{property_name}'.")

Example: Setting Multiple Properties

You can set multiple properties by calling set_custom_property multiple times.

# Set multiple custom properties
doc.set_custom_property("ApprovalStatus", "Approved")
doc.set_custom_property("Version", 2.1)
doc.set_custom_property("ReviewedBy", "John Doe")

Example: Boolean and Numeric Properties

The library supports various data types for custom properties, such as strings, integers, floats, and booleans.

# Boolean property
doc.set_custom_property("IsConfidential", True)

# Numeric property
doc.set_custom_property("DocumentID", 12345)

CLI Usage

The library also includes a command-line interface (CLI) for quickly managing properties.

Setting a Custom Property via CLI

docxprop set-custom example.docx ApprovalStatus Approved

Field Updates (Windows Only)

If you need to update fields in your Word document (e.g., updating a table of contents or other dynamic fields), use the update_fields method. This functionality requires pywin32 and is only available on Windows.

# Update fields in the document
if doc.update_fields():
    print("Fields were successfully updated.")
else:
    print("Field updating failed. Ensure pywin32 is installed.")

Advanced Features

Filtering Properties

You can filter custom properties by type and value.

# Get all boolean custom properties with the value False
false_properties = doc.filter_custom_properties(filter_type="boolean", filter_value=False)

for name, prop in false_properties.items():
    print(f"Property: {name}, Value: {prop.value}")

Reading Core Properties

Access core metadata such as the document title, creator, and modification date.

core_properties = doc.get_core_properties()
for name, prop in core_properties.items():
    print(f"Core Property: {name}, Value: {prop.value} (Type: {prop.type})")

License

This project is licensed under the MIT License.

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_properties-0.2.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

docx_properties-0.2.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file docx_properties-0.2.2.tar.gz.

File metadata

  • Download URL: docx_properties-0.2.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for docx_properties-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1ef182d5158f044ef079d54e083914f4c1340dc4a005da4b4255aeb1eb224a7b
MD5 b1c25a7222138f7c510e3795480bef4a
BLAKE2b-256 e8ab124351156b3c954993386ab9e81f79b09f6399ebb8db50d2125d2a8a9ef3

See more details on using hashes here.

File details

Details for the file docx_properties-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for docx_properties-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b197cada400d4002e8d52cd14aa1c0063e035df0ca962aade5481cfcfdb0398f
MD5 aa4c53e5bd16a3ba9bfbb71315527840
BLAKE2b-256 6e6416dfe2a2ffb983ff7611d148578fef0e27542a228ad72d456e9d1ea2b426

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