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.
The following code snippet shows how to add XMP metadata to EPS file in Python:
1from aspose.page.eps import *
2from util import Util
3###############################################
4###### Class and Method declaration here ######
5###############################################
6
7# The path to the documents directory.
8data_dir = Util.get_data_dir_working_with_xmp_metadata_in_eps()
9# Initialize EPS file input stream
10ps_stream = open(data_dir + "add_input.eps", "rb",)
11# Create PsDocument instance from stream
12document = PsDocument(ps_stream)
13
14try:
15 # 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)
16 xmp = document.get_xmp_metadata()
17
18 # Check metadata values extracted from PS metadata comments and set up in new XMP metadata
19
20 # Get "CreatorTool" value
21 if xmp.contains("xmp:CreatorTool"):
22 print("CreatorTool: " + xmp.get_value("xmp:CreatorTool").to_string_value())
23
24 # Get "CreateDate" value
25 if xmp.contains("xmp:CreateDate"):
26 print("CreateDate: " + xmp.get_value("xmp:CreateDate").to_string_value())
27
28 # Get "format" value
29 if xmp.contains("dc:format"):
30 print("Format: " + xmp.get_value("dc:format").to_string_value())
31
32 # Get "title" value
33 if xmp.contains("dc:title"):
34 print("Title: " + xmp.get_value("dc:title").to_array()[0].to_string_value())
35
36 # Get "creator" value
37 if xmp.contains("dc:creator"):
38 print("Creator: " + xmp.get_value("dc:creator").to_array()[0].to_string_value())
39
40 # Get "MetadataDate" value
41 if xmp.contains("xmp:MetadataDate"):
42 print("MetadataDate: " + xmp.get_value("xmp:MetadataDate").to_string_value())
43
44 # Save EPS file with new XMP metadata
45
46 # Create ouput stream
47 with open(data_dir + "add_output.eps", "wb") as out_ps_stream:
48 # Save EPS file
49 document.save(out_ps_stream)
50
51finally:
52 ps_stream.close()
You can download examples and data files from GitHub.