day19 PDF文件操作

day19 PDF文件操作

一、pypdf2获取文件内容

from PyPDF2 import PdfFileReader

# 1.打开pdf文件
f = open('files/流畅的python.pdf', 'rb')

# 2.创建reader
reader = PdfFileReader(f)

# 3.获取页数
page_total = reader.getNumPages()
print(f'页数:{page_total}')

# 4.获取指定页的页面
page0 = reader.getPage(0)
print(page0)

# 5.获取页面的文本内容
content = page0.extractText()
print(content)

f.close()

二、pypdf2在文件中写入页面

from PyPDF2 import PdfFileReader, PdfFileWriter

# 1.以读的方式打开需要合并的两个pdf
reader1 = PdfFileReader(open('files/流畅的python.pdf', 'rb'))
reader2 = PdfFileReader(open('files/教育部学历证书电子注册备案表.pdf', 'rb'))

# 2.以写的方式打开一个空白的pdf
writer1 = PdfFileWriter()

# 3.遍历获取原文件的页面
for index in range(reader1.getNumPages()):
    page = reader1.getPage(index)
    if index % 2 == 0:
        writer1.addPage(page)


writer1.addBlankPage()  # 添加一个空白页

for index in range(reader2.getNumPages()):
    page = reader2.getPage(index)
    if index % 2 == 0:
        writer1.addPage(page)

# 4.将writer1中的数据写入到pdf文件中
f = open('files/new_pdf.pdf', 'wb')
writer1.write(f)
f.close()

三、pypdf2添加水印

from PyPDF2 import PdfFileWriter, PdfFileReader


# 1.打开需要添加水印的pdf
reader1 = PdfFileReader(open('files/流畅的python.pdf', 'rb'))

# 2.打开提供水印的pdf
water_reader = PdfFileReader(open('files/学习路线图水印.pdf', 'rb'))
# 获取提供水印的页面
water = water_reader.getPage(0)

# 3.创建writer
writer = PdfFileWriter()

for index in range(reader1.getNumPages()):
    # 获取原文件页面
    page = reader1.getPage(index)
    # 在原页面上添加水印
    page.mergePage(water)
    # 将添加过水印的页面添加到writer中
    writer.addPage(page)

f = open('files/new_pdf1.pdf', 'wb')
writer.write(f)
f.close()

四、批量添加水印

from PyPDF2 import PdfFileWriter, PdfFileReader
import os

file = os.listdir('files2')
water_reader = PdfFileReader(open('files/学习路线图水印.pdf', 'rb'))
water = water_reader.getPage(0)
for x in file:
    reader1 = PdfFileReader(open(f'files2/{x}', 'rb'))
    writer = PdfFileWriter()
    for index in range(reader1.getNumPages()):
        page = reader1.getPage(index)
        page.mergePage(water)
        writer.addPage(page)
    f = open(f'files3/{x}', 'wb')
    writer.write(f)
    f.close()

五、创建水印

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm

# 1.注册字体
# TTFont(字体名,字体文件路径)
pdfmetrics.registerFont(TTFont('font1', 'files/dd.ttf'))
pdfmetrics.registerFont(TTFont('font2', 'files/t2.ttf'))

# 2.创建画布(创建空白pdf)
empty_canvas = canvas.Canvas('files/water.pdf')

# 3.设置画布的字体
empty_canvas.setFont('font1', 150)

# 4.设置文字颜色
# rgb值得范围是0(0)~1(255)
empty_canvas.setFillColorRGB(1, 0, 0, 0.5)

# 5.旋转
empty_canvas.rotate(45)

# 6.添加文字
empty_canvas.drawString(8*cm, 1*cm, 'WangXin')

# 7.保存pdf文件
empty_canvas.save()
上一篇:CGB2102-京淘项目完结-总结day19-20


下一篇:【学习笔记】莫比乌斯反演