用Python解析XML(2)
My favorite is lxml which includes a version of ElementTree as well as a really nice piece of code that they call "objectify". This latter piece will basically take XML and turn it into a dot notation Python object. I'll be using it to do our parsing because it is so straight-forward, easy to implement and understand. As stated earlier, I'll be using Reportlab to do the PDF creation piece.
Here's a simple script. that will do everything we need:
001.from decimal import Decimal
002.from lxml import etree, objectify
003.
004.from reportlab.lib import colors
005.from reportlab.lib.pagesizes import letter
006.from reportlab.lib.styles import getSampleStyleSheet
007.from reportlab.lib.units import inch, mm
008.from reportlab.pdfgen import canvas
009.from reportlab.platypus import Paragraph, Table, TableStyle
010.
011.########################################################################
012.class PDFOrder(object):
013.""""""
014.
015.#----------------------------------------------------------------------
016.def __init__(self, xml_file, pdf_file):
017."""Constructor"""
018.self.xml_file = xml_file
019.self.pdf_file = pdf_file
020.
021.self.xml_obj = self.getXMLObject()
022.
023.#----------------------------------------------------------------------
024.def coord(self, x, y, unit=1):
025."""
026.# http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab
027.Helper class to help position flowables in Canvas objects
028."""
029.x, y = x * unit, self.height - y * unit
030.return x, y
031.
032.#----------------------------------------------------------------------
033.def createPDF(self):
034."""
035.Create a PDF based on the XML data
036."""
037.self.canvas = canvas.Canvas(self.pdf_file, pagesize=letter)
038.width, self.height = letter
039.styles = getSampleStyleSheet()
040.xml = self.xml_obj
041.
042.address = """
043.SHIP TO:
044.
045.
046.
047.%s
048.
049.%s
050.
051.%s
052.
053.%s
054.
055.
056.""" % (xml.address1, xml.address2, xml.address3, xml.address4)
057.p = Paragraph(address, styles["Normal"])
058.p.wrapOn(self.canvas, width, self.height)
059.p.drawOn(self.canvas, *self.coord(18, 40, mm))
060.
061.order_number = 'Order #%s ' %xml.order_number
062.p = Paragraph(order_number, styles["Normal"])
063.p.wrapOn(self.canvas, width, self.height)
064.p.drawOn(self.canvas, *self.coord(18, 50, mm))
065.
066.data = []
067.data.append(["Item ID", "Name", "Price", "Quantity", "Total"])
068.grand_total = 0
069.for item in xml.order_items.iterchildren():
070.row = []
071.row.append(item.id)
072.row.append(item.name)
073.row.append(item.price)
074.row.append(item.quantity)
075.total = Decimal(str(item.price)) * Decimal(str(item.quantity))
076.row.append(str(total))
077.grand_total += total
078.data.append(row)
079.data.append(["", "", "", "Grand Total:", grand_total])
080.t = Table(data, 1.5 * inch)
081.t.setStyle(TableStyle([
082.('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
083.('BOX', (0,0), (-1,-1), 0.25, colors.black)
084.]))
085.t.wrapOn(self.canvas, width, self.height)
086.t.drawOn(self.canvas, *self.coord(18, 85, mm))
087.
088.txt = "Thank you for your business!"
089.p = Paragraph(txt, styles["Normal"])
090.p.wrapOn(self.canvas, width, self.height)
091.p.drawOn(self.canvas, *self.coord(18, 95, mm))
092.
093.#----------------------------------------------------------------------
094.def getXMLObject(self):
095."""
096.Open the XML document and return an lxml XML document
097."""
098.with open(self.xml_file) as f:
099.xml = f.read()
100.return objectify.fromstring(xml)
101.
102.#----------------------------------------------------------------------
103.def savePDF(self):
104."""
105.Save the PDF to disk
106."""
107.self.canvas.save()
108.
109.#----------------------------------------------------------------------
110.if __name__ == "__main__":
111.xml = "order.xml"
112.pdf = "letter.pdf"
113.doc = PDFOrder(xml, pdf)
114.doc.createPDF()
115.doc.savePDF()
116.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-736309/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 用Python解析XML(1)PythonXML
- 用Python解析XML(3)PythonXML
- Python解析xml[xml.dom]PythonXML
- python之XML解析PythonXML
- python 的xml解析PythonXML
- Python XML解析之DOMPythonXML
- python使用SAX解析xmlPythonXML
- python XML 檔案解析PythonXML
- XML的JAVA 解析(一)(2) (轉)XMLJava
- 用c#生成xml字串及解析xml字串C#XML字串
- Python解析XML檔案生成HTMLPythonXMLHTML
- python 解析xml 檔案: SAX方式PythonXML
- python 解析xml 檔案: DOM 方式PythonXML
- Python解析xml大檔案(sax)PythonXML
- NPM酷庫051:xml2js,解析XML格式資料NPMXMLJS
- python 解析xml 檔案: Element Tree 方式PythonXML
- Xml解析XML
- iOS – XML解析iOSXML
- go 解析xmlGoXML
- xml php 解析XMLPHP
- iOS - XML解析iOSXML
- 【Java】解析xmlJavaXML
- Java解析XMLJavaXML
- jaxb解析xmlXML
- DOM解析XMLXML
- Struts2教程3:struts.xml常用配置解析XML
- solrconfig.xml 應用解析調優SolrXML
- 用dom4j,解析xml 最好、最方便!XML
- 用Java結合SAX 2.0 解析XML文件 (轉)JavaXML
- struts2中struts.xml和web.xml檔案解析及工作原理XMLWeb
- 175.XML解析XML
- C# XML解析C#XML
- 使用jdom解析XMLXML
- 解析xml筆記XML筆記
- XML解析技術XML
- xml解析之domXML
- XML資料解析XML
- 關於xml解析XML