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.1.tar.gz (10.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_properties-0.2.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docx_properties-0.2.1.tar.gz
  • Upload date:
  • Size: 10.6 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.1.tar.gz
Algorithm Hash digest
SHA256 4e4990ba551f318770577fcd050d354fa3b69bd46f3b4b1a831b6d0e5dede242
MD5 d11612eaefc8402b755a677cf6fdae76
BLAKE2b-256 5c446ae1cfed8a1fc2c20fc10119a2a60a4c2d99d868f97dafb568965c599594

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for docx_properties-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37dcbb0a79a709a886e0768cddda6ee80dd5b32a97761eb2d39e6e4664bed064
MD5 cda49cea41d0cbf6f1f428d5006efd83
BLAKE2b-256 c7792c01e4af21dc77a444deb4ef1ce4d1e6316f4fc02e718c2bcdc5da6fd467

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