Python to draw PDF files ~ Super Simple Applet

Source: Internet
Author: User


Python drawing pdf file

Project Introduction
This project is very simple, this project lesson, code not more than 40 lines, mainly using Urllib and Reportlab module, to generate a PDF file.

Reportlab Official documents

Http://www.reportlab.com/docs/reportlab-userguide.pdf
Let's look at the original data on this page:
Http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt

650) this.width=650; "src=" Http://anything-about-doc.qiniudn.com/handle_pdf_py/1-1-1.png "id=" AIMG_MYKSG "class=" Zoom "border=" 0 "height=" 850 "width=" 786 "/>
Code:

#-*- coding: utf-8 -*-# 1.  for downloading raw data import urllib# 2.  Use shapes to draw complex shapes The From reportlab.graphics.shapes import *# 3. chars package contains many common graphics from  reportlab.graphics.charts.lineplots import LinePlotfrom  reportlab.graphics.charts.textlabels import label# 4.  for rendering PDF files from  reportlab.graphics import renderpdfurl =  '  COMMENT_CHARS =  ' #: '    # 5.  initialize coordinates origin  drawing = drawing (400, 200) data = [ ] #  Used to store downloaded and filtered data # 6.  download data and extract valid data For line in urlopen (URL). ReadLines ():     if not line.isspace ()  and not line[0] in COMMENT_CHARS:         data.append ([Float (n)  for n in line.split ()])         # 7.  extracting data for drawing pred = [row[2] for row in data]high = [row[3] for row in data]low =  [row[4] for row in data]times = [row[0] + row[1]/12.0 for  row in data]lp = lineplot () lp.x = 50lp.y = 50lp.height =  125lp.width = 300# 8. zip () is a python built-in function that takes a series of iterated objects as parameters and packages the corresponding elements in the object into a tuple (tuple). It then returns a list of these tuples (lists). If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortest length in the parameter. Lp.data = [zip (times, pred),  zip (Times, high),  zip (Times, low)]lp.lines[0]. strokecolor = colors.bluelp.lines[1].strokecolor = colors.redlp.lines[2].strokecolor  = COLORS.GREENDRAWING.ADD (LP) Drawing.add (String (250, 150,  ' sunspots ', fontsize=14,  fillcolor=colors.red)) Renderpdf.drawtofile (drawing,  ' report3.pdf ',  ' sunspots ')

Description:
Comment 6:line.isspace () Determine if a row is a blank line comment 7:data Each element of this list is actually a list, filtering the data according to the following example:
650) this.width=650; "Src=" http://anything-about-doc.qiniudn.com /handle_pdf_py/1-1-2.png "id=" aimg_yr03w "class=" Zoom "border=" 0 "height=" five "width=" 449 "/>
in fact, it takes years (times=yr ), predictive data (pred=predicted), highest data (High=high), lowest data (low=low)
Note 8: Because the data that the drawing needs to use must be in a certain format, the data is assembled in zip: [[(,), (,) ...]


650) this.width=650; "src=" Http://anything-about-doc.qiniudn.com/handle_pdf_py/1-1-3.png "id=" Aimg_ kaw3g "class=" Zoom "border=" 0 "height=" 253 "width=" "/>",
program run Result:
650) this.width=650; " Src= "Http://anything-about-doc.qiniudn.com/handle_pdf_py/1-1-4.png" id= "Aimg_ieagi" class= "Zoom" border= "0" Height = "287" width= "612"/>


More basic courses, Project class welcome landing Laboratory Building Official website: http://www.shiyanlou.com


This article is from the "Lab Building" blog, please be sure to keep this source http://shiyanloucs.blog.51cto.com/9731842/1599701

Python to draw PDF files ~ Super Simple Applet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.