python反编译chm文件并生成pdf文件

本文介绍了一款使用Python编写的自动化工具,该工具能够将CHM格式的帮助文档分解并转换为PDF文件。通过深度优先搜索算法遍历指定目录下的所有文件,并利用pdfkit将HTML文件转换成PDF格式,实现帮助文档的批量转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 1 # -*- coding: utf-8 -*-
 2 
 3 import os
 4 import os.path
 5 import logging
 6 import pdfkit
 7 
 8 original_chm = r'C:\Users\hushaojun\Documents\canoe\Help01\CAPLfunctions.chm'
 9 root_dir = r'C:\Users\hushaojun\Documents\canoe\Help01\canoe\Topics'
10 seperator = os.sep
11 
12 #    HH.EXE -decompile <输出路径> <目标chm文件>
13 
14 logging.basicConfig(level=logging.WARNING,
15                     format='%(asctime)s %(pathname)s %(filename)s %(funcName)s [line:%(lineno)d] %(levelname)s %(message)s',
16                     datefmt='%Y-%m-%d %H:%M:%S',
17                     filename='all_file_name.log',
18                     filemode='w+')
19 
20 #深度优先算法
21 def recursionFunction(level, path):
22     for file_content in os.listdir(path):    #列举显示所有文件夹下的内容
23         #“-”用于显示目录层次结构
24         full_path_name = os.path.join(path, file_content)
25         print('-'*(level+1) + full_path_name)
26         
27         if os.path.isdir(full_path_name):    #如果是文件夹就递归显示
28             recursionFunction(level+1, full_path_name)
29         else:    #如果是文件那就要直接显示
30             (shotname, extension) = os.path.splitext(file_content)
31     
32             logging.error('文件全名为:' + full_path_name)
33             print('文件名为:' + shotname)
34             print('文件后缀名为:' + extension)
35             
36             pdf_file_name = os.path.join(path, shotname + '.pdf')
37             
38             if (not os.path.exists(pdf_file_name)) and ((extension == '.htm') or (extension == '.html')):
39 try: 40 pdfkit.from_file(full_path_name, pdf_file_name) 41 except OSError: 42 print('just skip!') 43 except Exception as e: 44 print(type(e)) 45 46 if __name__ == '__main__': 47 decomplile_cmd = 'HH.EXE -decompile %s %s' % (root_dir, original_chm) 48 os.system(decomplile_cmd) 49 recursionFunction(0, root_dir)

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值