Trasforma XML tramite Python

Trasforma e carica XML in documento PDF. Usa Aspose.PDF per Python for .NET per modificare i documenti PDF a livello di codice

Aspose.PDF for Python for .NET Logo

Come trasformare XML con la libreria Python for .NET

Per trasformare un file XML in un file PDF, utilizza Aspose.PDF for Python via .NET, un’API potente e facile da usare. Apri PyPI, installalo e cerca aspose-pdf. In alternativa, esegui il comando:

Console

pip install aspose-pdf

Trasforma XML e caricalo in PDF tramite Python


È necessario Aspose.PDF for .NET per provare il codice nel proprio ambiente.

  1. Imposta i parametri della pagina.
  2. Carica il file XSLT.
  3. Quindi carica e trasforma.

Trasforma XML in PDF - Python

Questo codice di esempio mostra come trasformare XML in file PDF

import aspose.pdf as apdf

transform_xml_to_html(input_xml, xslt_string):
import xml.etree.ElementTree as ET
transform = ET.XSLT(ET.XML(xslt_string))
xml_tree = ET.ElementTree(ET.XML(input_xml))
result_tree = transform(xml_tree)
html_str = ET.tostring(result_tree, encoding='unicode', method='html')
from io import BytesIO
return BytesIO(html_str.encode('utf-8'))

example_xslt_to_pdf():
data_dir = "C:/tmp/"
with open("XMLFile1.xml", "r") as xml_file:
    xml_content = xml_file.read()
with open("XSLTFile1.xslt", "r") as xslt_file:
    xslt_content = xslt_file.read()

options = apdf.HtmlLoadOptions()
# Set page size to A5
options.page_info.height = 595
options.page_info.width = 420
pdf_document = apdf.Document(TransformXml.transform_xml_to_html(xml_content, xslt_content), options)
pdf_document.save(data_dir + "data_xml.pdf")