在Python中将xml转换为pdf

2024-04-23 21:46:27 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图将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文件读取?


Tags: 文件importreportpdffilesxmlbeopen
1条回答
网友
1楼 · 发布于 2024-04-23 21:46:27

我不确定我是否完全理解这个问题,但您是否希望pisa应用xslt转换?我不认为它能做到这一点(您可能需要查看lxml,并在使用pisa转换成pdf之前使用它来应用xslt

相关问题 更多 >