python 批量給 word,excel,ppt 或 pdf 文件添加水印


python 批量給 word 或 pdf 文件添加水印

項目地址:https://github.com/danpe1327/CheeseTool/tree/master/watermark

更新

  • 20200310 更新 README,對於中文顯示為方塊的問題,需使用宋體(simsun.ttc)、黑體(simhei.ttf)等字體。

1.依賴庫

pip install -r requirements.txt

2.部分python庫說明

  • comtypes,用於調用 word 等應用程序
  • reportlab,用於生成 pdf 水印文件

3.安裝pdf工具包

# PyPDF4,用於合成 pdf 文件
git clone https://github.com/danpe1327/PyPDF4.git pypdf4
cd pypdf4
python setup.py install --record files.txt

4.使用說明

命令

python add_watermark.py input_file 
                        --watermark DANPE
                        --angle 45
                        --font_file arial.ttf                        
                        --font_size 36
                        --color black
                        --alpha 0.2

# 參數說明 
            input_file 輸入單一文件或文件夾路徑,目前支持 word, excel, powerpoint 的新舊 6 種格式與 pdf 格式
           --watermark 水印文本,通過符號 ‘|’ 換行
           --angle 水印文本方向
           --font_file 可自定義字體文件,若無輸入或字體文件不存在,則使用默認的字體。對於中文無法顯示的問題,需要使用包含中文的字體,如宋體(simsun.ttc)、黑體(simhei.ttf)等。
           --font_size 字體大小
           --color 水印顏色,可選常見的顏色,如 [black, red, blue, green, yellow, white, gold, purple, pink, orange] 等
           --alpha 字體透明度
           --only_pdf 只轉換文本為 pdf,不添加水印
           --no_date 水印不加入日期
# 輸出
    若輸入為單一文件,會新建一個 wm-files 目錄,將添加水印的文件放置到該目錄下;
    若輸入為文件夾,則會遍歷目錄,將所有符合格式的文件添加水印,並新建一個 文件夾名+"-wm-files" 的目錄,存放結果。

5.常見錯誤

  • 轉換 ppt 文件時,出現錯誤 “The Python instance can not be converted to a COM object”

    在保存成 pdf 文件時,需要輸入參數 PrintRange

    office_file.ExportAsFixedFormat(pdf_file, 32, PrintRange=None)
    
  • 為中文文檔添加水印報錯 “'latin-1' codec can't encode characters in position 8-12: ordinal not in range(256)”
    修改pypdf4 的 utils.py,以支持中英文合成。
    代碼路徑: \PyPDF4\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:
            r = s.encode('utf-8')
            if len(s) < 2:
                bc[s] = r
            return r
    
  • win32 位系統下,出現 readerror
    修改pypdf4 的generic.py
    代碼路徑:\PyPDF4\的generic.py

    if not pdf.strict:
        warnings.warn(
            "Illegal character in Name Object", PdfReadWarning
        )
        return NameObject(name)
    else:    
        raise PdfReadError("Illegal character in Name Object")
修改為:
    try:
        if not pdf.strict:
            warnings.warn(
                "Illegal character in Name Object", PdfReadWarning
            )
            return NameObject(name)
        else:
            return NameObject(name.decode('gbk'))
    except Exception as e:
        raise PdfReadError("Illegal character in Name Object")

6.pdf 權限

  • -1 permit everything
  • -4096 deny anything
  • -4092 only print low resolution
  • -2044 only print high resolution
0000 0000 0001 unknown
0000 0000 0010 unknown
0000 0000 0011 unknown
0000 0000 0100 打印(低分辨率)
1000 0000 0100 打印(高分辨率)
0000 0000 1000 更改文檔、文檔組合、填寫表單域、簽名、創建模板頁面
0000 0001 0000 內容復制、復制內容用於輔助工具
0000 0010 0000 注釋、填寫表單域、簽名
0000 0100 0000 unknown
0000 1000 0000 unknown
0001 0000 0000 填寫表單域、簽名、創建模板頁面
0010 0000 0000 復制內容用於輔助工具
0100 0000 0000 文檔組合
1000 0000 0000 unknown


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM