【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}")
相关推荐
再玩一会儿看代码9 小时前
Java抽象类和接口区别_场景理解
java·开发语言·经验分享·笔记·python
大蚂蚁2号9 小时前
Python迭代器与生成器深度剖析:从底层协议到工程实战
python
专注搞钱9 小时前
AI编程实战:我用Python+LangChain搭建了一个半导体FAB智能运维Agent
python·langchain·ai编程
财经资讯数据_灵砚智能9 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年6月3日
大数据·人工智能·python·信息可视化·自然语言处理·灵砚智能
开源量化GO9 小时前
期货量化价差合约怎么订:天勤 SP 组合代码与订阅注意点
python·区块链
财经资讯数据_灵砚智能9 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年5月30日
人工智能·python·信息可视化·自然语言处理·ai编程·灵砚智能
AI玫瑰助手9 小时前
Python函数:global与nonlocal关键字的使用
开发语言·python·信息可视化
2301_764441339 小时前
Factorization Machine(FM模型,因子分解机)
python·算法
一天 24h9 小时前
Pinia 新手完全指南:从入门到精通的实战教程
前端·javascript·vue.js·pycharm·前端框架
绘梨衣5479 小时前
豆包Seed PDF解析企业落地方法论
大数据·python·pdf