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


Code:

#-*-coding:utf-8-*-#1. For downloading the original dataImportUrllib#2. Use shapes to draw complex shapes fromReportlab.graphics.shapesImport*#3. The chars package contains many common graphics fromReportlab.graphics.charts.lineplotsImportLinePlot fromReportlab.graphics.charts.textlabelsImportLabel#4. For rendering PDF files fromReportlab.graphicsImportRenderpdfurl='Http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt'Comment_chars='#:'#5. Initialize the origin of the coordinatesDrawing = Drawing (400, 200) Data= [ ]#for storing downloaded and filtered data.#6. Download data and extract valid data forLineinchUrlopen (URL). ReadLines ():if  notLine.isspace () and  notLINE[0]inchCOMMENT_CHARS:data.append ([Float (n) forNinchLine.split ()])#7. Extracting data for drawingPred = [Row[2] forRowinchData]high= [Row[3] forRowinchData]low= [Row[4] forRowinchData]times= [Row[0] + row[1]/12.0 forRowinchDATA]LP=LinePlot () lp.x= 50lp.y= 50Lp.height= 125Lp.width= 300#8. Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. 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
Note 6:line.isspace () determines whether a row is a blank line comment 7:data Each element of this list is actually a list, filtering the data based on:

What is needed is the year (times=yr), the forecast data (pred=predicted), the highest data (High=high), the lowest data (Low=low)
Note 8: Because the data used for drawing needs to be in a certain format, the data is assembled in zip: [[(,), (,) ...]



Program Run Result:



Today PO has a small program, very simple, we should practice practiced hand good ~ recently small series very distressed do not know what to continue to send something, if there are any suggestions can tell small series that ~ la La ~

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

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.