用Python解析XML(3)
Let's take a couple minutes to go over this code. First off is a bunch fo imports. This just sets up our environment with the needed compents from Reportlab and lxml. I also import the decimal module as I will be adding amounts and it is much more accurate for float mathematics than just using normal Python math.
Next we create our PDFOrder class which accepts two arguments: an xml file and a pdf file path. In our initialization method, we create a couple class properties, read the XML file and return an XML object. The coord method is for positioning Reportlab flowables, which are dynamic objects with the ability to split across pages and accept various styles. The createPDF method is the meat of the program. The canvas object is used to create our PDF and "draw" on it. I set it up to be letter sized and I also grab a default stylesheet.
Next I create a shipping address and position it near the top of the page, 18mm from the left and 40mm from the top. After that, I create and place the Order Number. Finally, I iterate over the items in the order and place them in a nested list, which is then placed in Reportlab's Table flowable.
Finally, I position the table and pass it some styles to give it a border and an inner grid. Lastly, we save the file to disk. The document is created and I've now got a nice prototype to show my colleagues. At this point, all I need to do is tweak the look and feel of the document by passing in different styles for the text (i.e. bold, italic, font size) or changing the layout a bit.
This is usually up to management or the client, so you'll have to wait and see what they want. Now you know how to parse an XML document in Python and create a PDF from the parsed data.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-736310/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 用Python解析XML(1)PythonXML
- 用Python解析XML(2)PythonXML
- Python解析xml[xml.dom]PythonXML
- python之XML解析PythonXML
- python 的xml解析PythonXML
- Python XML解析之DOMPythonXML
- python使用SAX解析xmlPythonXML
- python XML 檔案解析PythonXML
- XML的JAVA 解析(一)(3) (轉)XMLJava
- 用c#生成xml字串及解析xml字串C#XML字串
- Python解析XML檔案生成HTMLPythonXMLHTML
- python 解析xml 檔案: SAX方式PythonXML
- python 解析xml 檔案: DOM 方式PythonXML
- Python解析xml大檔案(sax)PythonXML
- python 解析xml 檔案: Element Tree 方式PythonXML
- Xml解析XML
- Python 3 解析 htmlPythonHTML
- 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
- 175.XML解析XML
- C# XML解析C#XML
- 使用jdom解析XMLXML
- 解析xml筆記XML筆記
- XML解析技術XML
- xml解析之domXML
- XML資料解析XML
- 關於xml解析XML
- 用Python解析XMindPython