python批量新增hexo文章封面
阿新 • • 發佈:2020-09-01
❝本文需要工具:
「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(別說你沒有奧!)
修改並輸入以下程式碼
importos
importio
importre
file_dir='E://sd//blog//1'#你的文章地址
num=0
defbatch_replace(file,num):
print('filename:%s'%file)
num=str(num)
content=""
strinfo=re.compile('[wodeitihuan]')
withio.open(os.path.join(file_dir,file),"r",encoding="utf-8",errors='ignore')asf:
forlineinf:
line=strinfo.sub(num,line)
content+=line
withio.open(os.path.join(file_dir,file),"w",encoding="utf-8",errors='ignore')asf:
f.write(content)
#遍歷目錄下檔案
forfolder,subFolder,filenamesinos.walk(file_dir):
print(file_dir)
forfilenameinfilenames:
#以.md結尾的檔案
ifos.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
**大功告成~**✌