Add XMP metadata to EPS file using Python
To add XMP metadata to an EPS file using Python, you need to follow these steps:
- Initialize an input stream for the input EPS file.
- Create an instance of PsDocument from the previously created input stream.
- Get an instance of XmpMetadata from the PsDocument. If the EPS file doesn’t contain XMP metadata, a new one will be created and populated with values from PS metadata comments, then returned to you.
- Now you can view the values of the metadata fields.
- Initialize an output stream for the output EPS file.
- Save EPS file with new XMP metadata.
Following code snippet shows how to add XMP metadata to EPS file in Python:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_xmp_metadata_in_eps()
3# Initialize EPS file input stream
4ps_stream = open(data_dir + "add_input.eps", "rb",)
5# Create PsDocument instance from stream
6document = PsDocument(ps_stream)
7
8try:
9 # Get XMP metadata. If EPS file doesn't contain XMP metadata we get new one filled with values from PS metadata comments (%%Creator, %%CreateDate, %%Title etc)
10 xmp = document.get_xmp_metadata()
11
12 # Check metadata values extracted from PS metadata comments and set up in new XMP metadata
13
14 # Get "CreatorTool" value
15 if xmp.contains("xmp:CreatorTool"):
16 print("CreatorTool: " + xmp.get_value("xmp:CreatorTool").to_string_value())
17
18 # Get "CreateDate" value
19 if xmp.contains("xmp:CreateDate"):
20 print("CreateDate: " + xmp.get_value("xmp:CreateDate").to_string_value())
21
22 # Get "format" value
23 if xmp.contains("dc:format"):
24 print("Format: " + xmp.get_value("dc:format").to_string_value())
25
26 # Get "title" value
27 if xmp.contains("dc:title"):
28 print("Title: " + xmp.get_value("dc:title").to_array()[0].to_string_value())
29
30 # Get "creator" value
31 if xmp.contains("dc:creator"):
32 print("Creator: " + xmp.get_value("dc:creator").to_array()[0].to_string_value())
33
34 # Get "MetadataDate" value
35 if xmp.contains("xmp:MetadataDate"):
36 print("MetadataDate: " + xmp.get_value("xmp:MetadataDate").to_string_value())
37
38 # Save EPS file with new XMP metadata
39
40 # Create ouput stream
41 with open(data_dir + "add_output.eps", "wb") as out_ps_stream:
42 # Save EPS file
43 document.save(out_ps_stream)
44
45finally:
46 ps_stream.close()
You can download examples and data files from GitHub.