Python XMP Toolkit
Python XMP Toolkit (version 2.1.0) provides Python bindings for Adobe's XMP Toolkit SDK by wrapping the Exempi C++ library. It enables reading, writing, and manipulating XMP (Extensible Metadata Platform) metadata in various file formats (e.g., JPEG, TIFF, PDF). The library maintains an active release cadence, with recent updates focusing on stability and compatibility with newer Exempi versions.
Common errors
-
ImportError: cannot import name 'XMPFiles' from 'xmp_toolkit'
cause Attempting to import from the old `xmp_toolkit` module after upgrading to version 2.0.0 or later.fixChange your import statements from `from xmp_toolkit import ...` to `from libxmp import ...`. -
libxmp.exempi.ExempiError: Exempi library not found.
cause The underlying C++ Exempi library is not installed or not discoverable in your system's library paths.fixInstall Exempi on your system. For Debian/Ubuntu: `sudo apt-get install libexempi-dev`. For macOS (Homebrew): `brew install exempi`. Ensure your system's dynamic linker can find the library. -
libxmp.exempi.ExempiError: Could not open file (no such file or directory)
cause The file specified for `XMPFiles` does not exist, or the provided path is incorrect, or there are insufficient permissions to access it.fixVerify the file path is correct and the file exists. Check file permissions for read/write access depending on your operation. Use absolute paths where possible.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes, primarily renaming the main module from `xmp_toolkit` to `libxmp` and overhauling the API. Code written for 1.x will not work directly with 2.x.
- gotcha This Python library requires the Exempi C++ library to be installed on your system. It is not bundled and will cause runtime errors if missing.
- gotcha When working with `XMPFiles`, ensure the target file path exists and that your application has the necessary read/write permissions. Opening files for update (e.g., `open_for_update=True`) requires write access.
Install
-
pip install python-xmp-toolkit
Imports
- XMPFiles
from xmp_toolkit import XMPFiles
from libxmp import XMPFiles
- XMPMeta
from xmp_toolkit import XMPMeta
from libxmp import XMPMeta
- consts
from xmp_toolkit import XMPConsts
from libxmp import consts
Quickstart
from libxmp import XMPMeta, consts
# Create a new XMP metadata object
xmp = XMPMeta()
# Set properties in the Dublin Core namespace
xmp.set_property(consts.NS_DC, "title", "My Awesome Photo")
xmp.set_property(consts.NS_DC, "creator", "Jane Doe")
# Add items to an array property (e.g., keywords)
xmp.append_array_item(consts.NS_DC, "subject", "landscape", None, consts.XMP_ARRAY_LAST_ITEM)
xmp.append_array_item(consts.NS_DC, "subject", "mountains", None, consts.XMP_ARRAY_LAST_ITEM)
# Get a property
title = xmp.get_property(consts.NS_DC, "title")
print(f"Title: {title}")
# Iterate over array items
print("Subjects:")
for i in range(xmp.count_array_items(consts.NS_DC, "subject")):
# Array items are 1-indexed in XMP
item = xmp.get_array_item(consts.NS_DC, "subject", i + 1)
print(f"- {item}")
# Serialize XMP to RDF/XML string
rdf_string = xmp.dump_as_rdf()
print("\n--- XMP as RDF ---")
print(rdf_string)
print("------------------")