def water_():
pdfmetrics.registerFont(TTFont('font1', 'files/bb.ttf'))
pdf = canvas.Canvas('files/water.pdf')
pdf.setFont('font1', 50)
pdf.setFillColorRGB(238/255,221/255,130/255,1)
pdf.rotate(30)
pdf.drawString(200, 50, '陈仲亮')
pdf.save()
def file_(path):
pdf_list=[]
files=os.listdir(f'{path}')
for file_name in files:
if file_name[-4:] == '.pdf':
pdf_list.append(file_name) #取出是pdf格式的文件名字,组成个列表
return pdf_list
if __name__ == '__main__':
water_()
all_pdfs = file_('pdfs')
print(all_pdfs)
water_reader = PdfFileReader(open('files/water.pdf', 'rb'))
water_page = water_reader.getPage(0)
writer = PdfFileWriter() #弄个空白页
for pdf in all_pdfs:
file_reader = PdfFileReader(open(fr'pdfs\{pdf}', 'rb'))
for index in range(file_reader.numPages):
page1 = file_reader.getPage(index)
page1.mergePage(water_page) # 合并水印页和原文件页
writer.addPage(page1)
writer.write(open(fr'pdfs\new_files{pdf}', 'wb'))
day21-PDF文件操作作业
最新推荐文章于 2024-08-23 16:53:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)