04-12 周五基于VS code + Python实现CSDN发布文章的自动生成

简介

 之前曾经说过,在撰写文章之后,需要,同样需要将外链的图像转换为的形式,因此,可以参考

04-12 周五 基于VS Code + Python 实现单词的自动提取

配置步骤

配置task

 在vscode的命令面板configure task。配置如下的任务

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "img handle",
            "type": "shell",
            "command": "python",
            "args": [
                "D:\\400-工作\\420-资源\\423-script\\replace_image_to_html.py", "${file}"
            ],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },

        {
            "label": "word extractor",
            "type": "shell",
            "command": "python",
            "args": [
                "D:\\400-工作\\420-资源\\423-script\\word_extractor.py", "${file}"
            ],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

创建了img handler

撰写处理脚本

 脚本内容如下所示:

import re
import sys

print(sys.argv)
print(f"len of argv:{len(sys.argv)}")
# 确保文件路径正确
file_path = sys.argv[1]

# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as file:
    content = file.read()
print(f"内容长度: {len(content)}")
# 定义正则表达式并执行替换
new_content = re.sub( r'!\[(.*)\]\((.*?)\)', r'<img src="\2", alt="\1">', content)

print(new_content)

# 将新内容写回文件
with open(file_path, 'w', encoding='utf-8') as file:
    file.write(new_content)

print(f"Replaced Markdown images with HTML in {file_path}")

上述代码逻辑中,使用re模块演示了组替换的概念

使用方式

使用方式,也是在VScode打开写好的博客txt内容,然后击出命令面板,选择task:run,选择img handle,即可文本的替换。

没有什么太神奇的东西,简单记录下。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 02:06:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 02:06:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 02:06:04       82 阅读
  4. Python语言-面向对象

    2024-04-13 02:06:04       91 阅读

热门阅读

  1. 迷宫-蓝桥602-bfs-2019省赛

    2024-04-13 02:06:04       39 阅读
  2. spispi

    spispi

    2024-04-13 02:06:04      31 阅读
  3. 【C++】C++11介绍

    2024-04-13 02:06:04       35 阅读
  4. 软件工程师,如何搞副业赚钱

    2024-04-13 02:06:04       37 阅读
  5. 朝花夕拾 - 写在 29

    2024-04-13 02:06:04       34 阅读
  6. js的包装类型

    2024-04-13 02:06:04       33 阅读
  7. SpringFramework实战指南(八)

    2024-04-13 02:06:04       32 阅读
  8. springboot项目如何配置跨域?

    2024-04-13 02:06:04       33 阅读
  9. 关于pr的第一次听写的笔记

    2024-04-13 02:06:04       37 阅读
  10. 数据结构-树状数组

    2024-04-13 02:06:04       35 阅读
  11. 【蓝桥杯-牛客冲刺题单】

    2024-04-13 02:06:04       49 阅读