在 EPS 的 XMP 元数据中添加 XML 命名空间 |Python
要将 XML 命名空间添加到 EPS 文件的 XMP 元数据中,您需要执行以下步骤:
- 为输入 EPS 文件初始化输入流。
- 从先前创建的输入流创建 PsDocument 实例。
- 从 PsDocument 获取 XmpMetadata 实例。如果 EPS 文件不包含 XMP 元数据,则会创建一个新的实例,并使用 PS 元数据注释中的值进行填充,然后返回给您。
- 现在,您可以向元数据添加新的 XML 命名空间。
- 为输出 EPS 文件初始化输出流。
- 使用更新后的 XMP 元数据保存 EPS 文件。
以下代码示例演示如何使用 Python 将 XML 命名空间添加到 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_simple_props_input.eps", "rb",)
5# Create a PsDocument instance from stream
6document = PsDocument(ps_stream)
7
8try:
9 # Get XMP metadata. If EPS file doesn't contain any 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 new XML namespace "tmp".
15 xmp.register_namespace_uri("tmp", "http://www.some.org/schema/tmp#")
16
17 # Add a new string property in new namespace.
18 xmp.add("tmp:newKey", XmpValue("NewValue"))
19
20 # Save the EPS file with the changed XMP metadata
21
22 # Create an ouput stream
23 with open(data_dir + "add_namespace_output.eps", "wb") as out_ps_stream:
24 # Save EPS file
25 document.save(out_ps_stream)
26
27finally:
28 ps_stream.close()
从 GitHub 检查并下载此示例以及其他示例和数据文件。