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.1.1.tar.gz (9.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.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docx_properties-0.1.1.tar.gz
  • Upload date:
  • Size: 9.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.1.1.tar.gz
Algorithm Hash digest
SHA256 d53317dc12085967419aa87f4a4c817b06a896e0daa70a152596a6c9d9ff4742
MD5 8c82c510b5fe3021a91408c2d120a38f
BLAKE2b-256 249a3c8fec1157324e8e2dcbe4e35e7bad575e76dc0f92320e4b69dff6bbca80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for docx_properties-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e946ac64fd615f35bd7dd4e6c60cd94109653d0ddff5deebcb3a257088dd72c
MD5 f43fe9c23c7b0358a508c475d6c9b4f4
BLAKE2b-256 a544e9e65b298d1d55397d4ad20a3e5ca44ed913a4d5b6398a5bf4fbc5e8220c

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