❝本文需要工具:
「excel」
「python3.x」
❞
今天突然覺得,我的部落格的文章更新這麼多了竟然還沒有一個封面,覺得首頁相當低調了…

正好皮膚帶有文章封面功能,所以我覺得要將文章批量加上文章推圖!
1.尋找圖片
推薦大家一個網站:https://www.pexels.com/
找到你想要的頁面:

然後無腦ctrl+s….:

按照型別分組然後將jpeg或者你想要的檔案留下,
全選按f2
輸入tuiutu
按一下enter
叮叮:

這還沒完,因為檔案中有空格,所以再批量改個名:

A組是原來檔名,B組為改之後的檔名
「注意:一定要加引號」
C1填寫函式:
="ren "&A2&" "&B2
然後拉下來,複製到txt裡,改成bat然後按一下就修改成功啦!
最後將圖片上傳到你的圖床。
2.批量新增cover屬性
因為hexo的文章是運用markdown書寫,相當靈活。
觀察每篇文章:

我們只需要在上面插入cover: XXXX,就可以設定好封面了。
因為本人的博文雖然不多,但也將近50篇,所以打工肯定是不可能打工的
觀察每篇文章的title和三個橫線之間的格式都是不變的,
所以我們只需要運用強大的vs以及正則化批量替換:

正則化程式碼:
搜尋:
---
title:
替換:
---
cover: https://圖床.com/tuitu[wodeitihuan].jpg
title:
注意,[wodeitihuan]不能少,等會要運用python批量替換。
3.批量替換cover圖片
還記得[wodeitihuan]嘛?
現在他派上用場了
開啟你的python(別說你沒有奧!)
修改並輸入以下程式碼
import os
import io
import re
file_dir = 'E://sd//blog//1' #你的文章地址
num=0
def batch_replace(file,num):
print('filename:%s' % file)
num=str(num)
content = ""
strinfo = re.compile('[wodeitihuan]')
with io.open(os.path.join(file_dir, file), "r", encoding="utf-8", errors='ignore') as f:
for line in f:
line = strinfo.sub(num, line)
content += line
with io.open(os.path.join(file_dir, file), "w", encoding="utf-8", errors='ignore') as f:
f.write(content)
# 遍歷目錄下檔案
for folder, subFolder, filenames in os.walk(file_dir):
print(file_dir)
for filename in filenames:
# 以 .md 結尾的檔案
if os.path.splitext(filename)[1] == '.md':
# 執行域名替換
print(os.path.splitext(filename)[1])
num=1+num
batch_replace(filename,num)
print("{} 替換完成".format(filename))
ok,開啟文章後你會發現每個cover的連結都變成了:
https://圖床.com/tuitu「某個數字」.jpg
**大功告成~**✌
❝求個關注~謝謝啦
❞