使用 Python 从 EPS 文件获取 XMP 元数据
要从 EPS 文件中提取 XMP 元数据,需要执行以下步骤:
- 为输入的 EPS 文件初始化输入流。
- 从先前创建的输入流创建 PsDocument 实例。
- 从 PsDocument 获取 XmpMetadata 实例。如果 EPS 文件不包含 XMP 元数据,则会创建一个新的实例,并使用 PS 元数据注释中的值进行填充,然后返回给您。
- 现在您可以访问和查看元数据字段的值了。
以下代码片段演示了如何使用 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 + "get_input.eps", "rb",)
5# Create 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 # Get "CreatorTool" value
13 if xmp.contains("xmp:CreatorTool"):
14 print("CreatorTool: " + xmp.get_value("xmp:CreatorTool").to_string_value())
15
16 # Get "CreateDate" value
17 if xmp.contains("xmp:CreateDate"):
18 print("CreateDate: " + xmp.get_value("xmp:CreateDate").to_string_value())
19
20 # Get "format" value
21 if xmp.contains("dc:format"):
22 print("Format: " + xmp.get_value("dc:format").to_string_value())
23
24 # Get "DocumentID" value
25 if xmp.contains("xmpMM:DocumentID"):
26 print("DocumentID: " + xmp.get_value("xmpMM:DocumentID").to_string_value())
27
28finally:
29 ps_stream.close()
您可以从 GitHub 检查和下载所有示例和数据文件。