Aggiungi metadati XMP a un file EPS usando Python
Per aggiungere metadati XMP a un file EPS utilizzando Python, è necessario seguire questi passaggi:
- Inizializzare un flusso di input per il file EPS di input.
- Creare un’istanza di PsDocument dal flusso di input creato in precedenza.
- Ottenere un’istanza di XmpMetadata dal PsDocument. Se il file EPS non contiene metadati XMP, ne verrà creato uno nuovo, popolato con i valori dei commenti dei metadati PS, e restituito all’utente.
- Ora è possibile visualizzare i valori dei campi dei metadati.
- Inizializzare un flusso di output per il file EPS di output.
- Salvare il file EPS con i nuovi metadati XMP.
Il seguente frammento di codice mostra come aggiungere metadati XMP a un file EPS in Python:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_xmp_metadata_in_eps()
3# Initialize EPS file input stream
4ps_stream = open(data_dir + "add_input.eps", "rb",)
5# Create PsDocument instance from stream
6document = PsDocument(ps_stream)
7
8try:
9 # Get XMP metadata. If EPS file doesn't contain 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 # Check metadata values extracted from PS metadata comments and set up in new XMP metadata
13
14 # Get "CreatorTool" value
15 if xmp.contains("xmp:CreatorTool"):
16 print("CreatorTool: " + xmp.get_value("xmp:CreatorTool").to_string_value())
17
18 # Get "CreateDate" value
19 if xmp.contains("xmp:CreateDate"):
20 print("CreateDate: " + xmp.get_value("xmp:CreateDate").to_string_value())
21
22 # Get "format" value
23 if xmp.contains("dc:format"):
24 print("Format: " + xmp.get_value("dc:format").to_string_value())
25
26 # Get "title" value
27 if xmp.contains("dc:title"):
28 print("Title: " + xmp.get_value("dc:title").to_array()[0].to_string_value())
29
30 # Get "creator" value
31 if xmp.contains("dc:creator"):
32 print("Creator: " + xmp.get_value("dc:creator").to_array()[0].to_string_value())
33
34 # Get "MetadataDate" value
35 if xmp.contains("xmp:MetadataDate"):
36 print("MetadataDate: " + xmp.get_value("xmp:MetadataDate").to_string_value())
37
38 # Save EPS file with new XMP metadata
39
40 # Create ouput stream
41 with open(data_dir + "add_output.eps", "wb") as out_ps_stream:
42 # Save EPS file
43 document.save(out_ps_stream)
44
45finally:
46 ps_stream.close()
È possibile scaricare esempi e file di dati da GitHub.