ziw2pdf

 1 import pdfkit
 2 import zipfile
 3 import os
 4 import sys
 5 import re
 6 
 7 # 出現亂碼時解碼
 8 def recode(raw: str) -> str:
 9     try:
10         return raw.encode('cp437').decode('gbk')
11     except:
12         return raw.encode('utf-8').decode('utf-8')
13 
14 def saveFile(htmlobj,pdfobj):
15     options = {
16         'page-size':'Letter','margin-top':'0.75in','margin-right':'0.75in',
17         'margin-bottom':'0.75in','margin-left':'0.75in','encoding':"UTF-8",
18         'custom-header': [('Accept-Encoding','gzip')],
19         'cookie': [('cookie-name1','cookie-value1'),('cookie-name2','cookie-value2'),],
20         "enable-local-file-access": None,
21         "header-left" : "收集人:馮際成 QQ:604756218 ",
22         "header-right" : "[date] 機密文件請勿外傳",
23         "header-spacing" : 3,
24         "footer-spacing" : 3,
25         "footer-center" : "- 第 [page] 頁-",
26         "header-line":None,
27         "page-size": 'A4',
28         "outline-depth":10,
29     }
30     config = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe') 
31     pdfDir = os.path.dirname(pdfobj)
32     if not os.path.exists(pdfDir):
33         os.makedirs(pdfDir)
34     pdfkit.from_file(htmlobj,pdfobj,options=options,configuration=config,verbose=True)
35 
36 def ziw2pdf(root,file):
37 
38     current_directory = os.path.dirname(os.path.abspath(__file__)) #獲得當前路徑
39     pathZip = os.path.join(root,file)
40     #開始合合成路徑
41     split_list=pathZip.replace(".ziw","").split("\\")[7:]
42     head=current_directory+r"\tmp"
43     if not os.path.exists(head):
44         os.makedirs(head)
45     for path in split_list:
46         head = os.path.join(head, re.sub(r"\s+", "", path))
47     ziwobj    = head
48     
49     split_list=pathZip.replace(".ziw",".pdf").split("\\")[7:]
50     head=current_directory+r"\pdf"
51     if not os.path.exists(head):
52         os.makedirs(head)
53     for path in split_list:
54         head = os.path.join(head, re.sub(r"\s+", "", path))
55     pdfobj = head
56     zipFile = zipfile.ZipFile(pathZip)          # 壓縮包路徑
57     zipFileList = zipFile.namelist()            # 獲取壓縮包裏所有文件
58     for f in zipFileList:
59         zipFile.extract(f, ziwobj)                 # 循環解壓文件到指定目錄
60         name1 = os.path.join(ziwobj, f)            # 亂碼文件名
61         name2 = os.path.join(ziwobj, recode(f))    # 解碼後文件名
62         os.rename(name1, name2)                 # 文件重命名
63     zipFile.close()                             # 關閉文件釋放內存
64     htmlobj =os.path.join(ziwobj ,"index.html")
65     saveFile(htmlobj,pdfobj)
66 
67 if __name__ == '__main__':
68     ziw_list = []
69     file_dir = "C:\\Users\\fengjicheng\\Documents\\My Knowledge\\Data\\[email protected]\\SAP"
70     #file_dir = "C:\\Users\\fengjicheng\\Documents\\My Knowledge\\Data\\[email protected]\\AI"
71     for root,dirs,files in os.walk(file_dir):
72         for file in files:
73             if file.endswith(".ziw"):
74                 ziw2pdf(root,file)
75         

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章