在 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 an EPS file input stream
4ps_stream = open(data_dir + "add_named_value_input.eps", "rb",)
5# Create PsDocument instance from 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 XMP metadata values
13
14 # Add a named value to "xmpTPg:MaxPageSize" structure.
15 xmp.add_named_value("xmpTPg:MaxPageSize", "stDim:newKey", XmpValue("NewValue"))
16
17 # Save the EPS file with changed XMP metadata
18
19 # Create an ouput stream
20 with open(data_dir + "add_named_value_output.eps", "wb") as out_ps_stream:
21 # Save EPS file
22 document.save(out_ps_stream)
23
24finally:
25 ps_stream.close()
您可以从 GitHub下载所有示例和数据文件。