python解析xml文件为pdf_在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("")

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文件读取?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值