【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}")
相关推荐
30955666631 分钟前
股票数据落库前的去重与主键设计:一次 SQLite 写入实录
java·开发语言·jvm·数据库·redis·python·sqlite
CyrusYiVPP35 分钟前
决策树背答案背到 24 片叶子?剪枝三刀砍回 5 片,测试集从 0.749 涨回 0.845,ML数学基础第13期
人工智能·python
用户298698530141 小时前
Python 批量处理 Word 水印:文字与图片水印的添加及移除
python·api
承渊政道1 小时前
【Python编程快速上手—让繁琐工作自动化】(字典和结构化数据:用键值对搭出棋盘和游戏背包)
python·游戏·pycharm·自动化·字典和结构化数据
Wellijohn1 小时前
让 AI 能改数据库,但不能背着你改:我给 MCP Server 装了一道审批闸
python·架构·mcp
AI创界者1 小时前
【无标题】Python 进阶实战(五十九章):分布式限流、熔断降级与背压控制实战
人工智能·python·aigc
山楂树の2 小时前
Python 函数:参数、作用域、Lambda 与类型注解
开发语言·python
炸薯条!2 小时前
Python装饰器详解:从闭包原理到语法糖,再到多个装饰器
开发语言·python
Zlzxzw2 小时前
Windows 安装WSL
python
Wellijohn14 小时前
我把公司内部的 12 个 CRUD 接口交给了 AI Agent,然后省出了两周
人工智能·python·mcp