用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解析PythonXML
- Python XML解析之DOMPythonXML
- python XML 檔案解析PythonXML
- Python解析XML檔案生成HTMLPythonXMLHTML
- Xml解析XML
- Java解析XMLJavaXML
- go 解析xmlGoXML
- iOS – XML解析iOSXML
- 175.XML解析XML
- C# XML解析C#XML
- 用Python解析XMindPython
- jdom解析xml檔案XML
- XML 檔案解析實踐 (DOM 解析)XML
- 面試官問我:如何在 Python 中解析和修改 XML面試PythonXML
- XML解析-最快的方式SAXXML
- 使用DocumentBuilderFactory解析XML淺談UIXML
- XML DOM 解析器概述XML
- 使用 Java 解析XML檔案JavaXML
- ArkTS 中的 XML 解析與生成:應用開發實戰XML
- Python3 裝飾器解析Python
- Python3 生成器解析Python
- Python3 迭代器深入解析Python
- 用 python 修改二進位制的 AndroidManifest.xmlPythonAndroidXML
- Tomcat 7 中 web 應用載入原理(二)web.xml 解析TomcatWebXML
- 搞懂 XML 解析,徒手造 WEB 框架XMLWeb框架
- 【spring原始碼系列】之【xml解析】Spring原始碼XML
- NPM酷庫051:xml2js,解析XML格式資料NPMXMLJS
- 【Go學習筆記16】解析xmlGo筆記XML
- XML DOM 解析器錯誤概述XML
- Python xml.etree.ElementTree讀寫xml檔案例項PythonXML
- 【Python 3】keras.layers.Lambda解析與使用PythonKeras
- java使用jaxb解析XML(含根據xml自動生成實體類)JavaXML
- XML Web 服務技術解析:WSDL 與 SOAP 原理、應用案例一覽XMLWeb
- Python學習——xml模組PythonXML
- springMVC---配置檔案解析(web.xml)SpringMVCWebXML
- 基於 DOM 的 XML 檔案解析類XML
- c#(解析xml檔案基礎方法)C#XML
- 如何使用 ABAP 程式碼解析 XML 檔案XML
- Python3 解析複雜結構的 jsonPythonJSON