gen_min_html.py 771 B

123456789101112131415161718192021222324
  1. from htmlmin import minify
  2. import sys
  3. def conpress(file_path, target_path):
  4. # 读取 HTML 文件
  5. with open(file_path, 'r', encoding='utf-8') as f:
  6. # 写入压缩后的 HTML 文件
  7. with open(target_path, 'w', encoding='utf-8') as t:
  8. t.write(minify(f.read(), remove_comments=True, remove_empty_space=True))
  9. if __name__ == "__main__":
  10. """
  11. 压缩指定的 HTML 文件。
  12. :param path/to/file: 欲读取的源文件路径
  13. :param path/to/file: 欲写入的目标文件路径
  14. """
  15. if len(sys.argv) != 3:
  16. print("使用方法:python gen_min_html.py <源文件路径> <目标文件路径>")
  17. sys.exit(1)
  18. file_path = sys.argv[1]
  19. target_path = sys.argv[2]
  20. conpress(file_path, target_path)