這篇文章主要介紹了如何用python給pdf批量添加水印并加密,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司專注于米東企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站定制開發(fā)。米東網(wǎng)站建設(shè)公司,為米東等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)1.設(shè)置路徑
import os os.getcwd() os.chdir('E:\\python\\test\\pdf批量加水印\\')
先設(shè)置路徑,把需要加水印的相關(guān)文檔放入一個目錄下。我的目錄是:E:\python\test\pdf批量加水印
os.chdir('E:\\python\\test\\pdf批量加水印\\')
2.準(zhǔn)備水印pdf文件
from reportlab.pdfgen import canvas from reportlab.lib.units import cm from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont pdfmetrics.registerFont(TTFont('song', 'C:/Windows/Fonts/simsun.ttc'))#宋體 from PyPDF2 import PdfFileWriter,PdfFileReader import xlrd def create_watermark(content): #默認(rèn)大小為21cm*29.7cm c = canvas.Canvas('mark.pdf', pagesize = (30*cm, 30*cm)) c.translate(10*cm, 10*cm) #移動坐標(biāo)原點(坐標(biāo)系左下為(0,0))) c.setFont('song',22)#設(shè)置字體為宋體,大小22號 c.setFillColorRGB(0.5,0.5,0.5)#灰色 c.rotate(45)#旋轉(zhuǎn)45度,坐標(biāo)系被旋轉(zhuǎn) c.drawString(-7*cm, 0*cm, content) c.drawString(7*cm, 0*cm, content) c.drawString(0*cm, 7*cm, content) c.drawString(0*cm, -7*cm, content) c.save()#關(guān)閉并保存pdf文件
系統(tǒng)默認(rèn)識別英文作為水印,但若水印為中文會無法顯示。解決辦法是先
from reportlab.pdfbase.ttfonts import TTFont
然后找到電腦中字體路徑,如我希望找到宋體,路徑為“C:/Windows/Fonts/simsun.ttc”,命名為"song"(如下圖所示,其他字體也可任君挑選)。
應(yīng)用到后續(xù)create_watermarkh函數(shù)中即可:
c.setFont('song',22)#設(shè)置字體為宋體,大小22號
另,希望頁面上貼四個水印,通過函數(shù)
c.drawString(-7*cm, 0*cm, content)
改變坐標(biāo)重復(fù)4次便可實現(xiàn)。由此最終生成水印pdf文件。
3.準(zhǔn)備水印pdf文件
def add_watermark2pdf(input_pdf,output_pdf,watermark_pdf): watermark = PdfFileReader(watermark_pdf) watermark_page = watermark.getPage(0) pdf = PdfFileReader(input_pdf,strict=False) pdf_writer = PdfFileWriter() for page in range(pdf.getNumPages()): pdf_page = pdf.getPage(page) pdf_page.mergePage(watermark_page) pdf_writer.addPage(pdf_page) pdfOutputFile = open(output_pdf,'wb') pdf_writer.encrypt('scb2018')#設(shè)置pdf密碼 pdf_writer.write(pdfOutputFile) pdfOutputFile.close()
只要安裝了該安裝的模塊,這一步驟基本沒有什么問題,提醒給pdf設(shè)置密碼的語法為
.encrypt('scb2018')#設(shè)置pdf密碼
若需更改密碼,改變引號中內(nèi)容即可。注:input_pdf為需要打上水印的pdf,watermark_pdf為水印pdf,output_pdf為最終輸出的pdf。
4.準(zhǔn)備水印pdf文件
ExcelFile = xlrd.open_workbook('商家名單.xlsx') sheet=ExcelFile.sheet_by_name('Sheet2')#打開有商家名單那個sheet print('———————已導(dǎo)入商家名單———————') col = sheet.col_values(3)#第4列內(nèi)容為商家名稱 id = sheet.col_values(0)#第1列內(nèi)容為ID del col[0];del id[0]#去掉標(biāo)題 id2 = [str(int(i)) for i in id] merchant_as_mark_content =[(i+' ')*4 if len(i)<=5 else i for i in col]#如果名稱太短則重復(fù)4個為一行
我是放在一個excel中的,截圖入下,需要把第4列商家名稱作為水印內(nèi)容印到目標(biāo)pdf上,對應(yīng)代碼為
sheet.col_values(3)
5.調(diào)用函數(shù)最終批量生成想要的pdf
if __name__=='__main__': for i,j,k in zip(merchant_as_mark_content,,id2):#i制作水印,j文件名,k對應(yīng)ID create_watermark(i)#創(chuàng)造了一個水印pdf:mark.pdf add_watermark2pdf('需要加水印的源文件.pdf',k+'通知('+j+').pdf','mark.pdf') print('———————已制作好第'+k+'個pdf,正在準(zhǔn)備下一個———————') print('———————所有文件已轉(zhuǎn)化完畢———————')
調(diào)用本步驟時我遇到一個錯誤
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 8-9: ordinal not in range(256)
說什么latin-1不能編碼字符,是個編碼問題。解決辦法:找到PyPDF2下utils.py的238行,我的路徑為:D:\Program Files (x86)\Python\lib\site-packages\PyPDF2\utils.py。然后把
r = s.encode('latin-1')
替換為如下代碼即可
try: r = s.encode('latin-1') if len(s) < 2: bc[s] = r return r except Exception as e: print(s) r = s.encode('utf-8') if len(s) < 2: bc[s] = r return r
到此所有程序已梳理完畢,所遇問題已解決,大家就可以愉快的打水印了!我出來的效果
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享如何用python給pdf批量添加水印并加密內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)網(wǎng)站設(shè)計公司行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!
當(dāng)前名稱:如何用python給pdf批量添加水印并加密-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://www.chinadenli.net/article44/dhsoee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、App開發(fā)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容