john пре 8 месеци
родитељ
комит
10ecadc092
4 измењених фајлова са 117 додато и 0 уклоњено
  1. 1 0
      .gitignore
  2. 3 0
      README.md
  3. 70 0
      create_hhp_file.py
  4. 43 0
      eccode.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+全书

+ 3 - 0
README.md

@@ -0,0 +1,3 @@
+获取所有的数据
+
+extract_chmLib 色库全书\(新\).chm 全书

+ 70 - 0
create_hhp_file.py

@@ -0,0 +1,70 @@
+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)

+ 43 - 0
eccode.py

@@ -0,0 +1,43 @@
+import os
+from chardet import detect
+
+def detect_file_encoding(file_path):
+    """
+    自动检测文件编码。
+    :param file_path: 文件路径
+    :return: 检测到的编码
+    """
+    with open(file_path, 'rb') as f:
+        raw_data = f.read(1024)  # 读取部分数据进行检测
+    result = detect(raw_data)
+    return result.get('encoding', 'utf-8')  # 默认返回 utf-8
+
+def convert_encoding_in_dir(directory, dest_encoding):
+    """
+    转换指定目录下所有文件的编码。
+    :param directory: 目录路径
+    :param dest_encoding: 目标编码
+    """
+    for root, dirs, files in os.walk(directory):
+        for file in files:
+            file_path = os.path.join(root, file)
+            try:
+                # 自动检测文件编码
+                src_encoding = detect_file_encoding(file_path)
+
+                # 读取并转换文件内容
+                with open(file_path, 'r', encoding=src_encoding, errors='ignore') as f:
+                    content = f.read()
+
+                # 写入目标编码
+                with open(file_path, 'w', encoding=dest_encoding) as f:
+                    f.write(content)
+
+                print(f"已成功转换文件编码: {file_path} (从 {src_encoding} 转到 {dest_encoding})")
+            except Exception as e:
+                print(f"无法处理文件 {file_path}: {e}")
+
+# 示例调用
+directory = "./全书"
+dest_encoding = "utf-8"
+convert_encoding_in_dir(directory, dest_encoding)