收录日期:2019/12/09 07:41:10 时间:2010-10-28 14:28:53 标签:python

I have a problem when I try to convert a XML file in a PDF file, here I’m going to explain briefly how I try to generate a PDF file.

We suppose I get the information from a database, then the code source is the following:

import pyodbc,time,os,shutil,types
import cStringIO
import ho.pisa as pisa
import urllib

def HTML2PDF(data, filename, open=False):

    """
    Simple test showing how to create a PDF file from
    PML Source String. Also shows errors and tries to start
    the resulting PDF
    """

    pdf = pisa.CreatePDF(
        cStringIO.StringIO(data),
        file(filename, "wb"))

    if open and (not pdf.err):
        os.startfile(str(filename))

    return not pdf.err


fout = open(BE_Full.xml","w")
fout.write("<?xml-stylesheet type='text/xsl' href='styles/Full_Report.xslt' alternate='no' title='Short' ?>")
fout.write("<files>")
fout.write("<validationreport>") 
fout.write("xmlvalidations/" + row.country + "_validation_" + row.dbversion + ".xml")
fout.write("</validationreport>")
fout.write("<reportformat>reports/EN_Report.xml</reportformat>")
fout.write("</files>")
fout.write
fout.close()
f =   urllib.urlopen("file:///C:/Documents%20and%20Settings/dmarban/Escritorio/python/BE_Full.xml")
s = f.read()
f.close()

HTML2PDF(s, "test.pdf", open=True)

The first I generate is an XML file that has the following content:

<?xml-stylesheet type='text/xsl' href='styles/Full_Report.xslt' alternate='no' title='Short' ?>
<files>
 <validationreport>xmlvalidations/BE_validation_mid2010.xml</validationreport>
 <reportformat>reports/EN_Report.xml</reportformat>
</files>

When I execute this code:

urllib.urlopen("file:///C:/Documents%20and%20Settings/dmarban/Escritorio/python/BE_Full.xml")
    s = f.read()
    f.close()
HTML2PDF(s, " BE_Full.pdf ", open=True)

It generates me the next file “BE_Full.pdf”, but instead of showing the contents of the folder “xmlvalidations/BE_validation_mid2010.xml” show me the contents of the labels that they are in pdf, It would show the following code:

xmlvalidations/BE_validation_mid2010.xml reports/EN_Report.xml

My question is, How I can parser a XML file in python, I read it as an HTML file?

I'm not sure I understand the question fully, but are you expecting pisa to apply the xslt transformation? I don't think it will do that (you might want to look at lxml and use that to apply the xslt before converting to pdf with pisa)