Python library for working with XMP (Extensible Metadata Platform) metadata in files.
—
The XMPMeta class provides comprehensive functionality for parsing, manipulating, and serializing XMP metadata. It supports all XMP data types including simple properties, arrays, structures, localized text, and provides complete namespace management.
The core metadata handling class that provides the main services of the library.
class XMPMeta:
def __init__(self, **kwargs):
"""
Create a new XMPMeta instance.
Parameters:
- xmp_str (optional): XMP string to parse
- _xmp_internal_ref (optional): Internal reference for library use
"""def get_property(self, schema_ns, prop_name):
"""
Get a property value.
Parameters:
- schema_ns: str, namespace URI
- prop_name: str, property name
Returns:
Property value if exists, None otherwise
Raises:
IOError: if exempi routine fails
"""
def get_array_item(self, schema_ns, array_prop_name, index):
"""
Get an array item value.
Parameters:
- schema_ns: str, namespace URI
- array_prop_name: str, array property name
- index: int, 1-based index
Returns:
Array item value
"""
def get_property_bool(self, schema, name):
"""Get property value as boolean."""
def get_property_int(self, schema_ns, name):
"""Get property value as integer."""
def get_property_long(self, schema_ns, prop_name):
"""Get property value as 64-bit integer."""
def get_property_float(self, schema_ns, prop_name):
"""Get property value as float."""
def get_property_datetime(self, schema_ns, prop_name):
"""Get property value as datetime object."""
def get_localized_text(self, schema_ns, alt_text_name, generic_lang, specific_lang):
"""
Get localized text from alt-text array.
Parameters:
- schema_ns: str, namespace URI
- alt_text_name: str, alt-text property name
- generic_lang: str, generic language code
- specific_lang: str, specific language code
Returns:
Localized text value
"""def set_property(self, schema_ns, prop_name, prop_value, **kwargs):
"""
Set a property value.
Parameters:
- schema_ns: str, namespace URI
- prop_name: str, property name
- prop_value: property value
- **kwargs: optional keyword arguments from XMP_PROP_OPTIONS
"""
def set_array_item(self, schema_ns, array_name, item_index, item_value, **kwargs):
"""
Set an array item value.
Parameters:
- schema_ns: str, namespace URI
- array_name: str, array property name
- item_index: int, 1-based index
- item_value: item value
- **kwargs: optional keyword arguments
"""
def append_array_item(self, schema_ns, array_name, item_value, array_options=None, **kwargs):
"""
Append item to array.
Parameters:
- schema_ns: str, namespace URI
- array_name: str, array property name
- item_value: value to append
- array_options: array options bitmask
- **kwargs: optional keyword arguments
"""
def set_property_bool(self, schema_ns, prop_name, prop_value, **kwargs):
"""Set property as boolean value."""
def set_property_int(self, schema_ns, prop_name, prop_value, **kwargs):
"""Set property as integer value."""
def set_property_long(self, schema_ns, prop_name, prop_value, **kwargs):
"""Set property as 64-bit integer value."""
def set_property_float(self, schema_ns, prop_name, prop_value, **kwargs):
"""Set property as float value."""
def set_property_datetime(self, schema_ns, prop_name, prop_value, **kwargs):
"""Set property as datetime value."""
def set_localized_text(self, schema_ns, alt_text_name, generic_lang, specific_lang, prop_value, **kwargs):
"""
Set localized text in alt-text array.
Parameters:
- schema_ns: str, namespace URI
- alt_text_name: str, alt-text property name
- generic_lang: str, generic language code
- specific_lang: str, specific language code
- prop_value: text value
- **kwargs: optional keyword arguments
"""def delete_property(self, schema_ns, prop_name):
"""Delete a property."""
def delete_localized_text(self, schema_ns, alt_text_name, generic_lang, specific_lang):
"""Delete localized text from alt-text array."""
def does_property_exist(self, schema_ns, prop_name):
"""
Check if property exists.
Returns:
bool: True if property exists
"""
def does_array_item_exist(self, schema_ns, array_name, item):
"""
Check if array item exists.
Returns:
bool: True if array item exists
"""
def count_array_items(self, schema_ns, array_name):
"""
Count items in array.
Returns:
int: Number of items in array
"""def parse_from_str(self, xmp_packet_str, xmpmeta_wrap=False, input_encoding=None):
"""
Parse RDF from string into XMP object.
Parameters:
- xmp_packet_str: str, XMP packet string
- xmpmeta_wrap: bool, whether string includes xmpmeta wrapper
- input_encoding: str, input encoding
"""
def serialize_to_str(self, padding=0, **kwargs):
"""
Serialize XMP to string.
Parameters:
- padding: int, padding for packet wrapper
- **kwargs: optional serialization options
Returns:
str: Serialized XMP packet
"""
def serialize_to_unicode(self, **kwargs):
"""
Serialize XMP to unicode string.
Returns:
unicode: Serialized XMP packet
"""
def serialize_and_format(self, padding=0, newlinechr='\n', tabchr='\t', indent=0, **kwargs):
"""
Serialize XMP with custom formatting.
Parameters:
- padding: int, padding for packet wrapper
- newlinechr: str, newline character
- tabchr: str, tab character
- indent: int, indentation level
- **kwargs: optional serialization options
Returns:
str: Formatted XMP packet
"""def clone(self):
"""
Create a copy of XMP packet.
Returns:
XMPMeta: New XMPMeta instance with copied data
"""@staticmethod
def get_prefix_for_namespace(namespace):
"""
Get namespace prefix for URI.
Parameters:
- namespace: str, namespace URI
Returns:
str: Namespace prefix
"""
@staticmethod
def get_namespace_for_prefix(prefix):
"""
Get namespace URI for prefix.
Parameters:
- prefix: str, namespace prefix
Returns:
str: Namespace URI
"""
@staticmethod
def register_namespace(namespace_uri, suggested_prefix):
"""
Register custom namespace.
Parameters:
- namespace_uri: str, namespace URI
- suggested_prefix: str, suggested prefix
Returns:
str: Actual registered prefix
"""Provides uniform iteration over XMP properties.
class XMPIterator:
def __init__(self, xmp_obj, schema_ns=None, prop_name=None, **kwargs):
"""
Create iterator for XMP object.
Parameters:
- xmp_obj: XMPMeta instance
- schema_ns (optional): str, namespace URI to restrict iteration
- prop_name (optional): str, property name to restrict iteration
- **kwargs: optional keyword arguments from XMP_ITERATOR_OPTIONS
"""
def __iter__(self):
"""Return iterator object."""
def __next__(self):
"""
Get next iteration item.
Returns:
tuple: (schema, name, value, options_dict)
"""
def skip(self, **kwargs):
"""
Skip portion of remaining iterations.
Parameters:
- **kwargs: keywords from XMP_SKIP_OPTIONS
"""from libxmp import XMPMeta
# Create XMP metadata object
xmp = XMPMeta()
# Set simple property
xmp.set_property("http://ns.adobe.com/xap/1.0/", "CreatorTool", "Python XMP Toolkit")
# Set localized text
xmp.set_localized_text(
"http://purl.org/dc/elements/1.1/",
"title",
"x-default",
"en-US",
"My Document Title"
)
# Get property
creator_tool = xmp.get_property("http://ns.adobe.com/xap/1.0/", "CreatorTool")
print(f"Creator Tool: {creator_tool}")
# Work with arrays
xmp.append_array_item(
"http://purl.org/dc/elements/1.1/",
"subject",
"keyword1"
)
xmp.append_array_item(
"http://purl.org/dc/elements/1.1/",
"subject",
"keyword2"
)
# Count array items
count = xmp.count_array_items("http://purl.org/dc/elements/1.1/", "subject")
print(f"Number of keywords: {count}")from libxmp import XMPMeta, XMPIterator
# Create and populate XMP
xmp = XMPMeta()
xmp.set_property("http://ns.adobe.com/xap/1.0/", "CreatorTool", "Python")
xmp.set_property("http://purl.org/dc/elements/1.1/", "creator", "John Doe")
# Iterate over properties
for schema, name, value, options in XMPIterator(xmp):
print(f"{schema}:{name} = {value}")
# Serialize to string
xmp_packet = xmp.serialize_to_str()
print(xmp_packet)
# Parse from string
new_xmp = XMPMeta()
new_xmp.parse_from_str(xmp_packet)Install with Tessl CLI
npx tessl i tessl/pypi-python-xmp-toolkit