Python實(shí)現(xiàn)給PDF添加水印的方法
利用 PyPDF2 處理 PDF 文件,相關(guān)文檔:https://pythonhosted.org/PyPDF2/
本文針對(duì) 僅有 PDF 文件,而無(wú)相關(guān) PDF 編輯器的情況下,給 PDF 添加水印。
一、前期準(zhǔn)備安裝 PyPDF2 ,命令提示框輸入:
pip install PyPDF2
新建 watermark.pdf 文件實(shí)際的水印,可以在此文件里修改水印文字的字體和位置。實(shí)現(xiàn)步驟: 新建 watermark.word ,【設(shè)計(jì)】 → to → 【水印】【自定義水印】 → to → 【文字水印】輸入【文字】,修改【字體】、【自號(hào)】、【顏色】和【板式】
效果
另存為 watermark.pdf
watermark.pdf 與 .py 文件放在同一目錄下
// =============================================# @Time : 2021-01-24# @Author : AXYZdong# @CSDN : https://axyzdong.blog.csdn.net/# @FileName: PDFset.py# @Software: Python IDE// =============================================from PyPDF2 import PdfFileReader, PdfFileWriterdef add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out): '''把水印添加到pdf中''' pdf_output = PdfFileWriter() input_stream = open(pdf_file_in, ’rb’) pdf_input = PdfFileReader(input_stream, strict=False) # 獲取PDF文件的頁(yè)數(shù) pageNum = pdf_input.getNumPages() # 讀入水印pdf文件 pdf_watermark = PdfFileReader(open(pdf_file_mark, ’rb’), strict=False) # 給每一頁(yè)打水印 for i in range(pageNum): page = pdf_input.getPage(i) page.mergePage(pdf_watermark.getPage(0)) page.compressContentStreams() # 壓縮內(nèi)容 pdf_output.addPage(page) pdf_output.write(open(pdf_file_out, ’wb’))if __name__ == ’__main__’: pdf_file_in = ’基于matlab的控制系統(tǒng)與仿真-1_AXYZdong的博客-CSDN博客.pdf’ pdf_file_out = ’watermarked.pdf’ pdf_file_mark = ’watermark.pdf’ add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out)
效果
// =============================================# @Time : 2021-01-24# @Author : AXYZdong# @CSDN : https://axyzdong.blog.csdn.net/# @FileName: PDFSet.py# @Software: Python IDE// =============================================from PyPDF2 import PdfFileReader, PdfFileWriterfrom reportlab.lib.units import cmfrom reportlab.pdfgen import canvasdef create_watermark(content): '''水印信息''' # 默認(rèn)大小為21cm*29.7cm file_name = 'mark.pdf' c = canvas.Canvas(file_name, pagesize=(30*cm, 30*cm)) # 移動(dòng)坐標(biāo)原點(diǎn)(坐標(biāo)系左下為(0,0)) c.translate(10*cm, 5*cm) # 設(shè)置字體 c.setFont('Helvetica', 30) # 指定描邊的顏色 c.setStrokeColorRGB(0, 1, 0) # 指定填充顏色 c.setFillColorRGB(0, 1, 0) # 旋轉(zhuǎn)45度,坐標(biāo)系被旋轉(zhuǎn) c.rotate(30) # 指定填充顏色 c.setFillColorRGB(0, 0, 0, 0.1) # 設(shè)置透明度,1為不透明 # c.setFillAlpha(0.1) # 畫幾個(gè)文本,注意坐標(biāo)系旋轉(zhuǎn)的影響 for i in range(5): for j in range(10): a=10*(i-1) b=5*(j-2) c.drawString(a*cm, b*cm, content) c.setFillAlpha(0.1) # 關(guān)閉并保存pdf文件 c.save() return file_namedef add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out): '''把水印添加到pdf中''' pdf_output = PdfFileWriter() input_stream = open(pdf_file_in, ’rb’) pdf_input = PdfFileReader(input_stream, strict=False) # 獲取PDF文件的頁(yè)數(shù) pageNum = pdf_input.getNumPages() # 讀入水印pdf文件 pdf_watermark = PdfFileReader(open(pdf_file_mark, ’rb’), strict=False) # 給每一頁(yè)打水印 for i in range(pageNum): page = pdf_input.getPage(i) page.mergePage(pdf_watermark.getPage(0)) page.compressContentStreams() # 壓縮內(nèi)容 pdf_output.addPage(page) pdf_output.write(open(pdf_file_out, ’wb’))if __name__ == ’__main__’: pdf_file_in = ’基于matlab的控制系統(tǒng)與仿真-1_AXYZdong的博客-CSDN博客.pdf’ pdf_file_out = ’watermarked.pdf’ pdf_file_mark = create_watermark(’CSDN@AXYZdong’) add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out)
效果
[1]:python pdf加水印[2]:Python中通過(guò)PyPDF2實(shí)現(xiàn)PDF添加水印
到此這篇關(guān)于Python實(shí)現(xiàn)給PDF添加水印的方法的文章就介紹到這了,更多相關(guān)Python PDF添加水印內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車3. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖4. .net如何優(yōu)雅的使用EFCore實(shí)例詳解5. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)6. python 爬取京東指定商品評(píng)論并進(jìn)行情感分析7. python基礎(chǔ)之匿名函數(shù)詳解8. Python獲取B站粉絲數(shù)的示例代碼9. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析10. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效
