实例1

需求背景

我写文章使用的是支持markdown语法的typora,这款工具方便写博客文章,最主要的是写完后可以通过mdnice转换节省不同平台排版的时间从而实现一篇文章快速投送多个平台,比如知乎,百家号,公众号等。但是一直以来我在把文章上传到公众号平台时,文章中的图片总是上传失败(原因就是我的文章中图片的图床与公众号连接的线路不好,在从我的图床上传图片到公众号时,对超过一定时间还没有上传成功的图片就会直接上传失败)。

插入失败

如上图,一共有29张图片其中28张图片插入失败。所以,我需要手动回到typora文件逐个把图片下载下来并且以阿拉伯数字从小到大排序,再按照顺序粘贴到公众号的对应位置。28张图片一个一个右键下载再重命名的话至少需要七八分钟,而且这种无聊重复的操作也会出错,可能一不小心就重复下载又或者命名错误又或者少下载了。由于懒就想到把重复无聊的操作交给程序来执行,而我又不会写程序,但是chagpt会写啊,把需求给它尝试看看,没想到成功了。

部分提问截图

代码不会我来写

不会用代码也可以问

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

需求(我的提问)

我有一个typora文件,文件里面有图片链接,我想把文件里面的图片依次下载下来并且以阿拉伯数字从小到大命名

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
import os
import re
import requests

# 读取Typora文件的内容
with open('your_typora_file.md', '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'Downloaded: {new_filename}')
else:
print(f'Failed to download: {img_link}')

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

在上述代码中,请将 'your_typora_file.md' 替换为您的Typora文件的实际路径。脚本会将下载的图片保存在一个名为 downloaded_images 的文件夹中,并以阿拉伯数字从小到大命名。

请注意,这只是一个简单的示例,您可能需要根据您的具体需求进行调整。此外,确保您已安装所需的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文件,以防万一。

效果

cmd运行过程

完成结果

窗口选择typora文件,无需修改代码中typora文件路径

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

# Create a Tkinter window
root = tk.Tk()
root.withdraw() # Hide the main window

# Ask the user to select a Typora file
typora_file_path = filedialog.askopenfilename(title="Select Typora File", filetypes=[("Markdown files", "*.md")])

# Check if a file was selected
if not typora_file_path:
print("No file selected. Exiting...")
exit()

# Read the content of the selected Typora file
with open(typora_file_path, 'r', encoding='utf-8') as file:
content = file.read()

# Use regular expression to find all image links
img_links = re.findall(r'!\[.*?\]\((.*?)\)', content)

# Create a directory to save downloaded images
if not os.path.exists('downloaded_images'):
os.mkdir('downloaded_images')

# Download and rename images
for i, img_link in enumerate(img_links):
response = requests.get(img_link)
if response.status_code == 200:
# Extract file extension
file_extension = os.path.splitext(img_link)[-1]
# Build a new filename
new_filename = f'downloaded_images/{i + 1}{file_extension}'
# Save the image
with open(new_filename, 'wb') as img_file:
img_file.write(response.content)
print(f'Downloaded: {new_filename}')
else:
print(f'Failed to download: {img_link}')

print('Image download completed.')