Change named value in XMP metadata of EPS|Python
In order to change named value in XMP metadata of EPS file you will need to take several 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 change the named value of structure metadata fileds.
- Initialize an output stream for the output EPS file.
- Save EPS file with new XMP metadata.
Here is a code snippet that shows how to change named value in XMP metadata in EPS file in Python:
1from aspose.page.eps import *
2from aspose.page.eps.xmp import *
3from util import Util
4###############################################
5###### Class and Method declaration here ######
6###############################################
7
8# The path to the documents directory.
9data_dir = Util.get_data_dir_working_with_xmp_metadata_in_eps()
10# Initialize a EPS file input stream
11ps_stream = open(data_dir + "add_named_value_input.eps", "rb",)
12# Create a PsDocument instance from the stream
13document = PsDocument(ps_stream)
14
15try:
16 # Get XMP metadata. If the EPS file doesn't contain XMP metadata we get a new one filled with values from PS metadata comments (%%Creator, %%CreateDate, %%Title etc)
17 xmp = document.get_xmp_metadata()
18
19 #Change the XMP metadata values
20
21 # Change the named value "stDim:unit" in "xmpTPg:MaxPageSize" structure.
22 xmp.set_named_value("xmpTPg:MaxPageSize", "stDim:unit", XmpValue("Inches"))
23
24 # Add the named value "stDim:newKey" in "xmpTPg:MaxPageSize" structure.
25 xmp.set_named_value("xmpTPg:MaxPageSize", "stDim:newKey", XmpValue("NewValue"))
26
27 # Save the EPS file with changed XMP metadata
28
29 # Create an ouput stream
30 with open(data_dir + "change_named_value_output.eps", "wb") as out_ps_stream:
31 # Save EPS file
32 document.save(out_ps_stream)
33
34finally:
35 ps_stream.close()
You can download examples and data files from GitHub.