【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}")
相关推荐
小兜全糖(xdqt)35 分钟前
python ppt转pdf以及图片提取
python·pdf·powerpoint
前端世界1 小时前
用Python打造智能成绩分析系统:从异常处理到断言验证的全流程实战
服务器·数据库·python
yaoxin5211231 小时前
229. Java 集合 - 操作集合中的多个元素(批量操作)
java·开发语言·python
岁岁岁平安1 小时前
python 配置pip镜像源
python
在人间负债^1 小时前
从Python到仓颉:核心项目内容迁移实践
开发语言·python·鸿蒙·仓颉
云和数据.ChenGuang1 小时前
SyntaxError: Non-UTF-8 code starting
人工智能·python·numpy
站大爷IP1 小时前
Airflow调度爬虫任务:从零搭建高效定时采集系统
python
站大爷IP1 小时前
Python高效实现Word转HTML:从基础到进阶的全流程方案
python
测试-鹏哥2 小时前
要将ITP集成到Jenkins Pipeline中,实现开发发版时自动触发自动化测试
运维·python·测试工具·ci/cd·jenkins
程序员三藏2 小时前
Postman接口测试详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman