如何将chm转换为html
hh-decompile 输出目录 chm文件将chm分解成多个需要hhc.exe 文件出品公司: Microsoft Corporation 文件所属产品: HTML HelpimportosimportsubprocessfrompathlibimportPath SOURCE_DIRrD:\xxx\常用文档\10outWORK_DIRrD:\xxxx\常用文档\splitHHC_EXErD:\hhc.exedeffind_html_files(root):递归查找所有 .html / .htm 文件files[]fordirpath,_,filenamesinos.walk(root):fornameinfilenames:ifname.lower().endswith((.html,.htm)):fullos.path.join(dirpath,name)relos.path.relpath(full,root)files.append(rel.replace(os.sep,/))returnsorted(files)defwrite_hhp(part_dir,chm_name,default_topic,files,hhc_name,hhk_name):hhp_pathPath(part_dir)/project.hhplines[[OPTIONS],Compatibility1.1 or later,fCompiled file{chm_name},fContents file{hhc_name},fIndex file{hhk_name},fDefault topic{default_topic},Display compile progressYes,Full-text searchYes,Language0x804,,[FILES],]lines.extend(files)hhp_path.write_text(\n.join(lines)\n,encodingutf-8)returnhhp_pathdefwrite_hhc_hhk(part_dir,name,files):同时生成 .hhc目录和 .hhk索引pathPath(part_dir)/name lines[!DOCTYPE HTML PUBLIC -//IETF//DTD HTML//EN,HTMLHEAD/HEADBODYUL,]forfinfiles:titlePath(f).stem lines.append(fLIOBJECT typetext/sitemapfparam nameName value{title}fparam nameLocal value{f}f/OBJECT)lines.append(/UL/BODY/HTML)path.write_text(\n.join(lines)\n,encodingutf-8)defcompile_chm(hhp_path,hhc_exe):hhc_exerD:\hhc\hhc.exeifnotos.path.isfile(hhc_exe):raiseFileNotFoundError(f找不到 hhc.exe:{hhc_exe})print(f正在编译:{hhp_path})resultsubprocess.run([hhc_exe,str(hhp_path)],stdoutsubprocess.PIPE,stderrsubprocess.PIPE,)outresult.stdout.decode(gbk,errorsignore)errresult.stderr.decode(gbk,errorsignore)print(stdout:,out)print(stderr:,err)print(returncode:,result.returncode)ifresult.returncode!0:raiseRuntimeError(f编译失败:{hhp_path})defsplit_chm(source_dir,work_dir,hhc_exe):work_dirPath(work_dir)source_dirPath(source_dir)ifnotsource_dir.is_dir():raiseFileNotFoundError(f解包目录不存在:{source_dir})print(1. 扫描 HTML 文件)html_filesfind_html_files(source_dir)iflen(html_files)2:raiseValueError(HTML 文件太少无法拆成两个 CHM)midlen(html_files)//2part1_fileshtml_files[:mid]part2_fileshtml_files[mid:]# 复制文件到两个子目录forpart_name,filesin[(part1,part1_files),(part2,part2_files)]:part_dirwork_dir/part_name part_dir.mkdir(parentsTrue,exist_okTrue)forfinfiles:srcsource_dir/f dstpart_dir/f dst.parent.mkdir(parentsTrue,exist_okTrue)ifsrc.exists():dst.write_bytes(src.read_bytes())# 生成 part1 工程print(2. 生成 part1 工程)p1work_dir/part1hhp1write_hhp(p1,part1.chm,part1_files[0],part1_files,part1.hhc,part1.hhk)write_hhc_hhk(p1,part1.hhc,part1_files)write_hhc_hhk(p1,part1.hhk,part1_files)# 生成 part2 工程print(3. 生成 part2 工程)p2work_dir/part2hhp2write_hhp(p2,part2.chm,part2_files[0],part2_files,part2.hhc,part2.hhk)write_hhc_hhk(p2,part2.hhc,part2_files)write_hhc_hhk(p2,part2.hhk,part2_files)# 编译print(4. 编译 part1.chm)compile_chm(hhp1,hhc_exe)print(5. 编译 part2.chm)compile_chm(hhp2,hhc_exe)print(完成输出目录,work_dir)if__name____main__:split_chm(SOURCE_DIR,WORK_DIR,HHC_EXE)