Obter metadados XMP de um ficheiro EPS utilizando Python
Para extrair metadados XMP de um ficheiro EPS, é necessário seguir várias etapas:
- Inicializar um fluxo de entrada para o ficheiro EPS de entrada.
- Criar uma instância de PsDocument a partir do fluxo de entrada criado anteriormente.
- Obter uma instância de XmpMetadata a partir do PsDocument. Se o ficheiro EPS não contiver metadados XMP, será criado um novo e preenchido com os valores dos comentários dos metadados PS e, em seguida, será devolvido a si.
- Agora pode aceder e visualizar os valores dos campos de metadados.
Abaixo encontra-se um excerto de código demonstrando como extrair metadados XMP de um ficheiro EPS em Python:
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()
You can examine and download all the examples and data files from GitHub.