【PyTest】玩转HTML报告:修改、汉化和优化

warning: 这篇文章距离上次修改已过185天,其中的内容可能已经有所变动。



# 导入必要的模块
import os
from pathlib import Path
from typing import List
 
# 定义修改HTML报告的函数
def modify_html_report(html_report_path: str, custom_css_path: str = None, custom_js_path: str = None, translation_dict: dict = None) -> None:
    """
    修改HTML报告文件,根据指定的CSS、JS和翻译字典进行自定义。
    :param html_report_path: HTML报告文件的路径。
    :param custom_css_path: 自定义CSS文件的路径。
    :param custom_js_path: 自定义JS文件的路径。
    :param translation_dict: 翻译词典,键为原文,值为翻译后的文本。
    """
    # 读取原始HTML内容
    with open(html_report_path, 'r', encoding='utf-8') as file:
        html_content = file.read()
    
    # 如果有CSS文件,插入到HTML中
    if custom_css_path and os.path.isfile(custom_css_path):
        with open(custom_css_path, 'r', encoding='utf-8') as css_file:
            css_content = css_file.read()
            html_content = html_content.replace('</head>', f'<style type="text/css">{css_content}</style></head>')
    
    # 如果有JS文件,在body结束前插入
    if custom_js_path and os.path.isfile(custom_js_path):
        with open(custom_js_path, 'r', encoding='utf-8') as js_file:
            js_content = js_file.read()
            html_content = html_content.replace('</body>', f'{js_content}</body>')
    
    # 如果有翻译字典,进行翻译替换
    if translation_dict:
        for original, translated in translation_dict.items():
            html_content = html_content.replace(original, translated)
    
    # 写入修改后的HTML内容
    with open(html_report_path, 'w', encoding='utf-8') as file:
        file.write(html_content)
 
# 示例调用
html_report_path = 'path/to/report.html'
custom_css_path = 'path/to/custom.css'
custom_js_path = 'path/to/custom.js'
translation_dict = {'Failed': '失败', 'Error': '错误', ...}  # 这里应包含所有需要翻译的文本
 
modify_html_report(html_report_path, custom_css_path, custom_js_path, translation_dict)

这个示例代码定义了一个modify_html_report函数,它接受HTML报告文件路径、自定义CSS文件路径、自定义JavaScript文件路径以及翻译字典作为参数。函数读取原始HTML内容,根据提供的文件路径插入自定义CSS和JavaScript,并对HTML中的特定文本进行翻译。最后,函数将修改后的HTML内容写回原报告文件。这个函数可以作为模板,用于自定义和汉化HTML报告。

评论已关闭

推荐阅读

DDPG 模型解析,附Pytorch完整代码
2024年11月24日
DQN 模型解析,附Pytorch完整代码
2024年11月24日
AIGC实战——Transformer模型
2024年12月01日
Socket TCP 和 UDP 编程基础(Python)
2024年11月30日
python , tcp , udp
如何使用 ChatGPT 进行学术润色?你需要这些指令
2024年12月01日
AI
最新 Python 调用 OpenAi 详细教程实现问答、图像合成、图像理解、语音合成、语音识别(详细教程)
2024年11月24日
ChatGPT 和 DALL·E 2 配合生成故事绘本
2024年12月01日
omegaconf,一个超强的 Python 库!
2024年11月24日
【视觉AIGC识别】误差特征、人脸伪造检测、其他类型假图检测
2024年12月01日
[超级详细]如何在深度学习训练模型过程中使用 GPU 加速
2024年11月29日
Python 物理引擎pymunk最完整教程
2024年11月27日
MediaPipe 人体姿态与手指关键点检测教程
2024年11月27日
深入了解 Taipy:Python 打造 Web 应用的全面教程
2024年11月26日
基于Transformer的时间序列预测模型
2024年11月25日
Python在金融大数据分析中的AI应用(股价分析、量化交易)实战
2024年11月25日
AIGC Gradio系列学习教程之Components
2024年12月01日
Python3 `asyncio` — 异步 I/O,事件循环和并发工具
2024年11月30日
llama-factory SFT系列教程:大模型在自定义数据集 LoRA 训练与部署
2024年12月01日
Python 多线程和多进程用法
2024年11月24日
Python socket详解,全网最全教程
2024年11月27日