【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}")
相关推荐
从小就看凹凸曼^o^6 小时前
Python基础5 - 字典与集合:(1)字典
开发语言·python
小赵AI手记6 小时前
技术拆解(十七)具身智能:机器人动作生成为何走向Diffusion Policy?
人工智能·笔记·python·机器人
本地化文档6 小时前
poethepoet-docs-l10n
python·github·gitcode·sphinx
baopixiaoz7 小时前
AI量化策略师|Web3 量化交易研究员
大数据·人工智能·python·区块链
Uncommon.7 小时前
使用Pytorch自动计算梯度
人工智能·pytorch·python
阿标的博客8 小时前
Python实训案例(二):输出植物证书
python
鸿芯微控科技8 小时前
压电点胶阀出胶量不稳定怎么排查?驱动波形、背压、点胶重量与Python分析
开发语言·python·压电点胶阀·精密点胶·点胶重量·流体控制
一位正在转型AI全栈的前端工程师8 小时前
从 0 到 1 搭建生产级 RAG 系统:切片、召回与重排序调优实录
python·前端工程化
糖炒栗子03268 小时前
learn-claude-code 简要记录
笔记·python
Logintern098 小时前
asyncio里面的await理解
开发语言·python