实例1

需求背景

我写文章使用的是支持markdown语法的typora,但是我在把文章上传到公众号平台时,文章中的图片总是上传失败(原因就是我的文章中图片的图床与公众号连接的线路不好,在从我的图床上传图片到公众号时,对超过一定时间还没有上传成功的图片就会直接上传失败)。

插入失败

如上图,一共有29张图片其中28张图片插入失败。所以,我需要手动回到typora文件逐个把图片下载下来并且以阿拉伯数字从小到大排序,再按照顺上传到文章对应位置。

部分提问截图

提问截图

不会用代码也可以问

补充一句:使用chatgpt提问时,要把它当作一个人来交流,要把自己的需求表达的完善充足,它回答的对要给予它肯定,错误也要指出,这样不断地训练它纠正它,它给出的答案就越准确。使用chatgpt的一个误区就是把它当成普通的搜索引擎使用!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import re
import requests
from tkinter import filedialog
import tkinter as tk

# 创建一个 Tkinter 窗口
root = tk.Tk()
root.withdraw() # 隐藏主窗口

# 让用户选择一个 Typora 文件
typora_file_path = filedialog.askopenfilename(title="选择 Typora 文件", filetypes=[("Markdown 文件", "*.md")])

# 检查是否选择了文件
if not typora_file_path:
print("未选择文件,退出...")
exit()

# 读取选定的 Typora 文件内容
with open(typora_file_path, 'r', encoding='utf-8') as file:
content = file.read()

# 使用正则表达式查找所有图片链接
img_links = re.findall(r'!\[.*?\]\((.*?)\)', content)

# 创建一个目录来保存下载的图片
if not os.path.exists('downloaded_images'):
os.mkdir('downloaded_images')

# 下载并重命名图片
for i, img_link in enumerate(img_links):
response = requests.get(img_link)
if response.status_code == 200:
# 提取文件扩展名
file_extension = os.path.splitext(img_link)[-1]
# 构建一个新文件名
new_filename = f'downloaded_images/{i + 1}{file_extension}'
# 保存图片
with open(new_filename, 'wb') as img_file:
img_file.write(response.content)
print(f'下载成功: {new_filename}')
else:
print(f'下载失败: {img_link}')

print('图片下载完成。')

功能是从用户选定的 Typora 文件中提取 Markdown 格式的图片链接,并下载这些图片到本地目录。

确保已安装所需的Python库,可以使用以下命令安装:

1
pip install requests

您可以使用上面提供的Python代码来下载Typora文件中的图片。以下是如何使用这个Python脚本的步骤:

  1. 复制上面的Python代码并粘贴到一个文本编辑器中(如Notepad++或Visual Studio Code)。

  2. 保存脚本文件并确保文件扩展名为.py(例如download_typora_images.py)。

  3. 打开命令行终端(例如cmd或终端)。

  4. 在终端中,导航到包含脚本文件的目录,使用cd命令切换到该目录,例如:

    1
    cd /path/to/script/directory
  5. 运行脚本,使用以下命令:

    1
    python download_typora_images.py

    确保您已安装了Python,并且python命令可以在命令行中正常运行。

  6. 脚本将开始执行,根据Typora文件中的图片链接下载图片,并将它们以阿拉伯数字从小到大命名,保存在名为downloaded_images的文件夹中。

请注意,脚本执行完成后,您将在脚本文件的目录中看到一个名为downloaded_images的文件夹,其中包含下载的图片。确保在运行脚本之前备份Typora文件,以防万一。

实例2

需求背景:hexo的_post文章目录下有50多个.md格式的文件,文件里面有图床图片链接,由于图床的域名需要更换,所以所有文件里面存在之前图床域名的图片都要替换成现在的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.simpledialog import Dialog
import re

# 自定义对话框类,用于查找和替换文本输入
class SearchReplaceDialog(Dialog):
def __init__(self, parent):
self.search_text = ""
self.replace_text = ""
self.use_regex = False
super().__init__(parent, title="查找和替换")

def body(self, master):
tk.Label(master, text="查找文本:").grid(row=0)
tk.Label(master, text="替换文本:").grid(row=1)

self.search_entry = tk.Entry(master)
self.replace_entry = tk.Entry(master)

self.search_entry.grid(row=0, column=1)
self.replace_entry.grid(row=1, column=1)

# 模式选择:全局替换 or 正则表达式
self.mode_var = tk.BooleanVar(value=False)
tk.Radiobutton(master, text="全局替换", variable=self.mode_var, value=False).grid(row=2, column=0, columnspan=2)
tk.Radiobutton(master, text="正则表达式", variable=self.mode_var, value=True).grid(row=2, column=1, columnspan=2)

return self.search_entry # 初始聚焦

def apply(self):
self.search_text = self.search_entry.get()
self.replace_text = self.replace_entry.get()
self.use_regex = self.mode_var.get()

# 主函数
def main():
# 创建主窗口并隐藏
root = tk.Tk()
root.withdraw()

# 选择文件夹
folder_path = filedialog.askdirectory(title="请选择文件夹")
if not folder_path:
print("未选择文件夹,操作取消。")
return

# 显示查找和替换对话框
dialog = SearchReplaceDialog(root)
search_text = dialog.search_text
replace_text = dialog.replace_text
use_regex = dialog.use_regex

if not search_text or not replace_text:
print("查找或替换文本为空,操作取消。")
return

# 遍历目录中的所有 .md 文件
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".md"):
file_path = os.path.join(root, file)

# 打开文件并读取内容,确保使用 UTF-8 编码
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

# 替换内容
if use_regex:
new_content = re.sub(search_text, replace_text, content)
else:
new_content = content.replace(search_text, replace_text)

# 如果内容发生变化,则写回文件
if content != new_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"替换成功:{file_path}")
else:
print(f"未发现替换内容:{file_path}")

print("替换操作完成!")

# 运行主函数
if __name__ == "__main__":
main()

代码功能:可以用于批量查找和替换 Markdown 文件内容