Home  >  Article  >  Backend Development  >  How to manipulate PDF using python

How to manipulate PDF using python

php中世界最好的语言
php中世界最好的语言Original
2018-04-09 14:49:312319browse

This time I will show you how to use python to operate PDF, and what are the precautions for using python to operate PDF. The following is a practical case, let's take a look.

Knowledge points involved

1. Use of urllib

2. Use of reportlab library

This example is really simple, but I found that in In python, you can write

for loop directly in the array [], which becomes more convenient the more you use it.

The following is the code:

from urllib import urlopen
from reportlab.graphics.shapes import *
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics import renderPDF
URL = 'http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt'
COMMENT_CHARS = '#:'
drawing = Drawing(400, 200)
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()])
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 = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp.data = [zip(times, pred),zip(times,high),zip(times, low)]
lp.lines[0].strokeColor = colors.blue
lp.lines[1].strokeColor = colors.red
lp.lines[2].strokeColor = colors.green
drawing.add(lp)
drawing.add(String(250,150, 'Sunspots',fontSize=14,fillColor=colors.red))
renderPDF.drawToFile(drawing, 'report3.pdf','Sunspots')
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Python opencv detects and extracts the target color

How does Python write the data in the data frame to the database

The above is the detailed content of How to manipulate PDF using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn