Aggiungi valore denominato nei metadati XMP di EPS | Python
Per aggiungere un valore denominato ai metadati XMP di un file EPS, 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, che verrà poi restituito all’utente.
- Ora è possibile aggiungere il valore denominato ai campi dei metadati della struttura.
- Inizializzare un flusso di output per il file EPS di output.
- Salvare il file EPS con i metadati XMP aggiornati.
Di seguito è riportato un frammento di codice che mostra come aggiungere un valore denominato ai metadati XMP in 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 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()
You can download all the examples and data files from GitHub.