当我试图将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("")
fout.write("")
fout.write("xmlvalidations/" + row.country + "_validation_" + row.dbversion + ".xml")
fout.write("")
fout.write("reports/EN_Report.xml")
fout.write("")
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' ?>
xmlvalidations/BE_validation_mid2010.xml
reports/EN_Report.xml
当我执行此代码时: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文件读取?