在 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 the EPS file input stream
4ps_stream = open(data_dir + "add_simple_props_input.eps", "rb",)
5# Create the PsDocument instance from the stream
6document = PsDocument(ps_stream)
7
8try:
9 # Get XMP metadata. If EPS file doesn't contain any 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 #Change XMP metadata values
13
14 # Add another title. It will be added at the end of array by default.
15 xmp.add_array_item("dc:title", XmpValue("NewTitle"))
16
17 # Add one more creator. It will be added in the array by an index (0).
18 xmp.add_array_item("dc:creator", 0, XmpValue("NewCreator"))
19
20 # Save the EPS file with changed XMP metadata
21
22 # Create an ouput stream
23 with open(data_dir + "add_array_items_output.eps", "wb") as out_ps_stream:
24 # Save the EPS file
25 document.save(out_ps_stream)
26
27finally:
28 ps_stream.close()您可以从 GitHub下载示例和数据文件。