import os import subprocess def create_hhp_file(project_dir, output_chm, default_topic, hhc_file, hhk_file): """ 创建 .hhp 项目文件 """ hhp_content = f""" [OPTIONS] Compatibility=1.1 or later Compiled file={output_chm} Contents file={hhc_file} Default topic={default_topic} Display compile progress=No Full-text search=Yes Index file={hhk_file} Language=0x804 中文(简体,中国) [FILES] """ # 添加所有 HTML 文件到项目中 for file in os.listdir(project_dir): if file.endswith(".html"): hhp_content += f"{file}\n" # 写入 .hhp 文件 hhp_file = os.path.join(project_dir, "MyProject.hhp") with open(hhp_file, "w", encoding="utf-8") as f: f.write(hhp_content.strip()) return hhp_file def compile_chm(hhp_file, html_help_workshop_path): """ 调用 HTML Help Workshop 编译 .chm 文件 """ try: # 调用 hhc.exe result = subprocess.run( [os.path.join(html_help_workshop_path, "hhc.exe"), hhp_file], capture_output=True, text=True ) if result.returncode == 0: print("CHM 文件编译成功!") else: print("编译失败,错误信息如下:") print(result.stderr) except FileNotFoundError: print("未找到 hhc.exe,请检查 HTML Help Workshop 的安装路径。") if __name__ == "__main__": # 项目目录路径 project_dir = r"C:\path\to\MyProject" # 修改为你的项目文件夹路径 # 输出的 CHM 文件名 output_chm = "MyProject.chm" # 默认 HTML 页面 default_topic = "file1.html" # 目录文件和索引文件 hhc_file = "toc.hhc" hhk_file = "index.hhk" # HTML Help Workshop 的安装路径 html_help_workshop_path = r"C:\Program Files (x86)\HTML Help Workshop" # 生成 .hhp 文件 hhp_file = create_hhp_file(project_dir, output_chm, default_topic, hhc_file, hhk_file) print(f".hhp 文件已生成:{hhp_file}") # 编译 .chm 文件 compile_chm(hhp_file, html_help_workshop_path)