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\\caoxiancc@live.cn\\SAP" 70 #file_dir = "C:\\Users\\fengjicheng\\Documents\\My Knowledge\\Data\\caoxiancc@live.cn\\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
作者:冯际成
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.如有问题,请与作者联系:604756218@qq.com