create_hhp_file.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import subprocess
  3. def create_hhp_file(project_dir, output_chm, default_topic, hhc_file, hhk_file):
  4. """
  5. 创建 .hhp 项目文件
  6. """
  7. hhp_content = f"""
  8. [OPTIONS]
  9. Compatibility=1.1 or later
  10. Compiled file={output_chm}
  11. Contents file={hhc_file}
  12. Default topic={default_topic}
  13. Display compile progress=No
  14. Full-text search=Yes
  15. Index file={hhk_file}
  16. Language=0x804 中文(简体,中国)
  17. [FILES]
  18. """
  19. # 添加所有 HTML 文件到项目中
  20. for file in os.listdir(project_dir):
  21. if file.endswith(".html"):
  22. hhp_content += f"{file}\n"
  23. # 写入 .hhp 文件
  24. hhp_file = os.path.join(project_dir, "MyProject.hhp")
  25. with open(hhp_file, "w", encoding="utf-8") as f:
  26. f.write(hhp_content.strip())
  27. return hhp_file
  28. def compile_chm(hhp_file, html_help_workshop_path):
  29. """
  30. 调用 HTML Help Workshop 编译 .chm 文件
  31. """
  32. try:
  33. # 调用 hhc.exe
  34. result = subprocess.run(
  35. [os.path.join(html_help_workshop_path, "hhc.exe"), hhp_file],
  36. capture_output=True,
  37. text=True
  38. )
  39. if result.returncode == 0:
  40. print("CHM 文件编译成功!")
  41. else:
  42. print("编译失败,错误信息如下:")
  43. print(result.stderr)
  44. except FileNotFoundError:
  45. print("未找到 hhc.exe,请检查 HTML Help Workshop 的安装路径。")
  46. if __name__ == "__main__":
  47. # 项目目录路径
  48. project_dir = r"C:\path\to\MyProject" # 修改为你的项目文件夹路径
  49. # 输出的 CHM 文件名
  50. output_chm = "MyProject.chm"
  51. # 默认 HTML 页面
  52. default_topic = "file1.html"
  53. # 目录文件和索引文件
  54. hhc_file = "toc.hhc"
  55. hhk_file = "index.hhk"
  56. # HTML Help Workshop 的安装路径
  57. html_help_workshop_path = r"C:\Program Files (x86)\HTML Help Workshop"
  58. # 生成 .hhp 文件
  59. hhp_file = create_hhp_file(project_dir, output_chm, default_topic, hhc_file, hhk_file)
  60. print(f".hhp 文件已生成:{hhp_file}")
  61. # 编译 .chm 文件
  62. compile_chm(hhp_file, html_help_workshop_path)