更改 EPS 文件的 XMP 元数据中的命名值 | Python
要更改 EPS 文件的 XMP 元数据中的命名值,您需要执行以下步骤:
- 为输入 EPS 文件初始化输入流。
- 从先前创建的输入流创建 PsDocument 实例。
- 从 PsDocument 获取 XmpMetadata 实例。如果 EPS 文件不包含 XMP 元数据,则会创建一个新的实例,并使用 PS 元数据注释中的值填充该实例,然后返回给您。
- 现在您可以更改结构元数据字段的命名值。
- 为输出 EPS 文件初始化输出流。
- 使用新的 XMP 元数据保存 EPS 文件。
这里有一个代码片段,展示了如何在 Python 中更改 EPS 文件中 XMP 元数据中的命名值:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_xmp_metadata_in_eps()
3# Initialize a EPS file input stream
4ps_stream = open(data_dir + "add_named_value_input.eps", "rb",)
5# Create a PsDocument instance from the stream
6document = PsDocument(ps_stream)
7
8try:
9 # 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)
10 xmp = document.get_xmp_metadata()
11
12 #Change the XMP metadata values
13
14 # Change the named value "stDim:unit" in "xmpTPg:MaxPageSize" structure.
15 xmp.set_named_value("xmpTPg:MaxPageSize", "stDim:unit", XmpValue("Inches"))
16
17 # Add the named value "stDim:newKey" in "xmpTPg:MaxPageSize" structure.
18 xmp.set_named_value("xmpTPg:MaxPageSize", "stDim:newKey", XmpValue("NewValue"))
19
20 # Save the EPS file with changed XMP metadata
21
22 # Create an ouput stream
23 with open(data_dir + "change_named_value_output.eps", "wb") as out_ps_stream:
24 # Save EPS file
25 document.save(out_ps_stream)
26
27finally:
28 ps_stream.close()
您可以从 GitHub下载示例和数据文件。