添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
+关注继续查看

前言
大家好,我是辣条

之前辣条有发布过我们常用的两个技能点,今天第三个技能点(PDF)他来了

《Python实例篇:自动操作Excel文件(既简单又特别实用)》

《Python技巧篇:如何巧妙运用Python处理Word文档》

PDF是Portable Document Format的缩写,这类文件通常使用 .pdf 作为其扩展名。在日常开发工作中,最容易遇到的就是从PDF中读取文本内容以及用已有的内容生成PDF文档这两个任务。

目录
前言
工具
从PDF中提取文本
旋转和叠加页面
加密PDF文件
创建PDF文件
总结
工具
python3.7

Pycharm

PDF

PyPDF2

reportlab

从PDF中提取文本
PyPDF2没有办法从PDF文档中提取图像、图表或其他媒体,但它可以提取文本,并将其返回为Python字符串。

import PyPDF2

reader = PyPDF2.PdfFileReader('test.pdf')
page = reader.getPage(0)
print(page.extractText())
1
2
3
4
5
旋转和叠加页面
​ 上面的代码中通过创建PdfFileReader对象的方式来读取PDF文档,该对象的getPage方法可以获得PDF文档的指定页并得到一个PageObject对象,通过PageObject对象的rotateClockwise和rotateCounterClockwise方法可以实现页面的顺时针和逆时针方向旋转,通过PageObject对象的addBlankPage方法可以添加一个新的空白页,代码如下所示。

import PyPDF2

from PyPDF2.pdf import PageObject

创建一个读PDF文件的Reader对象

reader = PyPDF2.PdfFileReader('resources/xxx.pdf')

创建一个写PDF文件的Writer对象

writer = PyPDF2.PdfFileWriter()

对PDF文件所有页进行循环遍历

for page_num in range(reader.numPages):

# 获取指定页码的Page对象
current_page = reader.getPage(page_num)  # type: PageObject
if page_num % 2 == 0:
    # 奇数页顺时针旋转90度
    current_page.rotateClockwise(90)
else:
    # 偶数页反时针旋转90度
    current_page.rotateCounterClockwise(90)
writer.addPage(current_page)

最后添加一个空白页并旋转90度

page = writer.addBlankPage() # type: PageObject
page.rotateClockwise(90)

通过Writer对象的write方法将PDF写入文件

with open('resources/xxx.pdf', 'wb') as file:

writer.write(file)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
加密PDF文件
​ 使用PyPDF2中的PdfFileWrite对象可以为PDF文档加密,如果需要给一系列的PDF文档设置统一的访问口令,使用Python程序来处理就会非常的方便。

import PyPDF2

reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
writer = PyPDF2.PdfFileWriter()
for page_num in range(reader.numPages):

writer.addPage(reader.getPage(page_num))

通过encrypt方法加密PDF文件,方法的参数就是设置的密码

writer.encrypt('foobared')
with open('resources/XGBoost-encrypted.pdf', 'wb') as file:

writer.write(file)

1
2
3
4
5
6
7
8
9
10
创建PDF文件
​ 创建PDF文档需要三方库reportlab的支持,使用 pip install reportlab 命令安装

from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas

pdf_canvas = canvas.Canvas('resources/python创建.pdf', pagesize=A4)
width, height = A4

绘图

image = canvas.ImageReader('resources/xxx.jpg')
pdf_canvas.drawImage(image, 20, height - 395, 250, 375)

显示当前页

pdf_canvas.showPage()

注册字体文件

pdfmetrics.registerFont(TTFont('Font1', 'resources/fonts/Vera.ttf'))
pdfmetrics.registerFont(TTFont('Font2', 'resources/fonts/青呱石头体.ttf'))

写字

pdf_canvas.setFont('Font2', 40)
pdf_canvas.setFillColorRGB(0.9, 0.5, 0.3, 1)
pdf_canvas.drawString(width // 2 - 120, height // 2, '你好,世界!')
pdf_canvas.setFont('Font1', 40)
pdf_canvas.setFillColorRGB(0, 1, 0, 0.5)
pdf_canvas.rotate(18)
pdf_canvas.drawString(250, 250, 'hello, world!')

保存

pdf_canvas.save()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

总结
​ 以上就是python对PDF文档的操作了,实际操作还需要大家多多练习。

Python获取指定目录下的所有文件路径、获取指定目录下所有文件名(但是不包含子目录中文件名)、获取指定目录下所有pdf文件名(但是不包含子目录中pdf文件名)
Python获取指定目录下的所有文件路径、获取指定目录下所有文件名(但是不包含子目录中文件名)、获取指定目录下所有pdf文件名(但是不包含子目录中pdf文件名)
使用python将word文档和pdf电子书进行格式互转(兼容Windows/Linux)
一些重要文档格式之间的互转在目前显得尤为重要,pdf作为通用格式在现在各个平台上兼容性是最好的,所以写python脚本将这些word文档批量转换pdf是最好的解决方案。 由于windows系统对于word文档有天然的兼容性优势,所以转换起来很简单,普遍上是通过comtypes模块。