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.0.tar.gz (11.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.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docx_properties-0.2.0.tar.gz
  • Upload date:
  • Size: 11.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.0.tar.gz
Algorithm Hash digest
SHA256 0209f3151d5df669cf563094c26b52b935ca6a1a08ef401afbb1c066e9998ca6
MD5 1a4ba338280f80d4fb756ed8525a16c8
BLAKE2b-256 928b260697d7b0df18b962a66a3e71b0c8c4b33dfb0bc01fdf0c5153fad096b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for docx_properties-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1068dd3606deaadc9d2b7373f70e8d6c42fcdbc92c37ebc3932d933ce2fa4b13
MD5 f2ace896f56ae32bc92dfb52b30dcd6f
BLAKE2b-256 d92e6ad245ec0af034045b54b5ca796e41ce4b397cfa1e229d580f687e900760

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