【python】python文件=注释=书写建议,基于pycharm

一个方法的备注,书写的好;

方便他人查看,方法的作用;

方便不进入方法就能查看,光标移动到方法上就行

方便查看传参,返回值

显得有 逼格吧!,规范化!

  • 建议如下格式书写,方便阅读查看~
python 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025-02-14 14:31
# @Author  : duxiaowei
# @File    : report_css_into_html.py
# @Software: 将报告中的css文件插入到html文件中,方便jira下载查看
import os


def rewrite_html(html_file_path):
    """将报告中的css文件插入到html文件中,方便上传jira,下载查看
        Args:
            html_file_path: 文件路径
        Returns:
            无
        Raises:
            写入失败
    """
    path_html = os.path.abspath('..') + '\\test_API\\reports\\' + html_file_path
    path_css = os.path.abspath('..') + '\\test_API\\reports\\assets\\style.css'
    try:
        # 读取 HTML 文件内容
        with open(path_html, 'r', encoding='GB2312') as html_file:
            html_content = html_file.read()

        # 读取 CSS 文件内容
        with open(path_css, 'r', encoding='GB2312') as path_css:
            css_content = path_css.read()

        # 定义要查找的 <link> 标签
        link_tag = '<link href="assets/style.css" rel="stylesheet" type="text/css"/>'

        # 生成新的 <style> 标签内容
        style_tag = f'<style>\n{css_content}\n</style>'

        # 替换 <link> 标签为 <style> 标签
        new_html_content = html_content.replace(link_tag, style_tag)

        # 将修改后的内容写回 HTML 文件
        with open(path_html, 'w', encoding='GB2312') as html_file:
            html_file.write(new_html_content)

        # print("CSS 文件内容已成功插入到 HTML 文件中。")
    except FileNotFoundError:
        print("未找到 HTML 或 CSS 文件,请检查文件路径。")
    except Exception as e:
        print(f"处理文件时出现错误: {e}")
相关推荐
河南凹凸环境艺术设计6 小时前
民宿酒店设计实力公司
大数据·人工智能·python
小江的记录本6 小时前
【AI Agent】《2026年9月 AI Agent全栈开发技术选型专项面试宝典》
java·spring boot·python·spring·ai·面试·ai编程
春涧草茶7 小时前
慢就是快-11-1数据容器 | 列表
python
维克兜率天7 小时前
【维克】技术指标是什么:从均线到MACD的数学原理
人工智能·笔记·python·算法·量化
卷无止境7 小时前
当FastAPI遇上机器学习,一个脚手架工具能省下多少工夫
后端·python·fastapi
卷无止境7 小时前
Kilo CLI 到底是什么
人工智能·后端·python
SurpriseDPD8 小时前
bpftrace‑tasks 工具代码分析
开发语言·python
BS30813_vx8 小时前
【编号:01652】Spring Boot村务管理系统:财务公开、党建活动与计生服务一体化实战
开发语言·python
边吃番茄边敲代码8 小时前
从本地工具到 MCP:给 Agent 接入独立工具服务,并补齐自动化测试
人工智能·python·功能测试·ai·单元测试·pytest
SugarAbsinthe9 小时前
【Agent开发实习小记】从人工逐条验证到自动化验收:新 API 接入前的效率瓶颈
人工智能·python·自动化·pytest