在Python中将XML转换为PDF
我在尝试把一个XML文件转换成PDF文件时遇到了问题,下面我简单说一下我是怎么生成PDF文件的。
假设我从数据库获取信息,下面是我的代码:
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)
我首先生成一个XML文件,里面的内容是这样的:
<?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>
当我执行这段代码时:
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)
它会生成一个名为“BE_Full.pdf”的文件,但这个文件并没有显示“xmlvalidations/BE_validation_mid2010.xml”文件夹里的内容,而是显示了PDF里标签的内容,具体是这样的:
xmlvalidations/BE_validation_mid2010.xml reports/EN_Report.xml
我想问的是,如何在Python中解析一个XML文件,让它像HTML文件那样读取?