day 21 作业(csv文件与PDF文件的操作)
作业:将指定文件夹中所有的pdf文件的所有也都添加属于自己的水印
import os
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from PyPDF2 import PdfFileReader, PdfFileWriter
flise_name=os.listdir('flises/pdfs')
pdfmetrics.registerFont(TTFont('font1','flise/青呱石头体.ttf'))
pdfmetrics.registerFont(TTFont('font2','flise/bb.ttf'))
pdfmetrics.registerFont(TTFont('font3','flise/dd.ttf'))
pdf=canvas.Canvas('flis2/water.pdf')
pdf.setFont('font2',30)
pdf.setFont('font3',30)
pdf.setFillColorRGB(1,0,0,0.5)
pdf.rotate(45)
pdf.drawString(100,-20,'最珍贵的,往往都是看不到的。')
pdf.drawString(120,20,'最珍贵的,往往都是看不到的。')
pdf.drawString(140,80,'最珍贵的,往往都是看不到的。')
pdf.save()
def add_shui(y):
water_reder=PdfFileReader(open('flis2/water.pdf','rb'))
flises_reader=PdfFileReader(open(f'flises/pdfs/{y}','rb'))
page_num=flises_reader.numPages
water_page=water_reder.getPage(0)
writer = PdfFileWriter()
for x in range(page_num):
pagex=flises_reader.getPage(x)
pagex.mergePage(water_page)
writer.addPage(pagex)
writer.write(open(f'flis2/{y}','wb'))
for y in flise_name:
add_shui(y)