确定需求

需求是将wordpress中的文章:

  • 标题作为文件名
  • 分类作为父文件夹名
  • 添加 front matter 字段

执行数据库查询

执行之前删除草稿箱,修订版,否则会造成数据重复。

打开数据库表管理软件,这里使用navicat

1
2
3
4
5
6
7
8
use DATABASE_NAME;
select
p.post_title,t.name,p.post_content
from wp_posts p, wp_term_relationships r,wp_terms t, wp_term_taxonomy tt
where p.id=r.object_id
and r.term_taxonomy_id=t.term_id
and tt.term_id=t.term_id
and tt.taxonomy='category';

导出查询结果

导出格式选择.csv,保存为posts.csv

image-20231228130116707

image-20231228130153203

处理数据

将下面保存为posts.py执行即可,执行完成后会在当前目录下按目录为文件夹、标题为文件名生成.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
import os
import csv

WORK_DIR = os.getcwd()


def set_post_dir(category):
post_dir = os.path.join(WORK_DIR, category)
if not os.path.exists(post_dir):
os.mkdir(post_dir)
os.chdir(post_dir)


with open('posts.csv', newline='', encoding='utf-8') as csvfile:
posts = csv.reader(csvfile)
for post in posts:
if post[1] == "name":
continue
post_title, category, post_content = post

set_post_dir(category)
try:
with open(post_title+".md", "w", encoding='utf-8') as post_file:
post_file.write(post_content)
except Exception as exp:
print(exp)
print(f"category: {category}")
print(f"title: {post_title}")
print(f"content: {post_content}")

添加 front matter 字段

将下面代码保存为frontmatter.py

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
import os
from datetime import date

# 获取当前日期
today = date.today().strftime("%Y-%m-%d")

# 遍历指定文件夹下的所有文件
folder_path = "指定文件夹路径"
for root, dirs, files in os.walk(folder_path):
for file_name in files:
# 获取文件的绝对路径
file_path = os.path.join(root, file_name)

# 获取文件所在文件夹的名称作为category
category = os.path.basename(os.path.dirname(file_path))

# 获取文件名作为title
title = os.path.splitext(file_name)[0]

# 读取文件内容
with open(file_path, "r", encoding='utf-8') as file:
content = file.read()

# 在文件开头添加front matter信息
front_matter = f"---\ntitle: \"{title}\"\ndate: {today}\ncategories:\n - {category}\n---\n\n"
content = front_matter + content

# 将修改后的内容写回文件
with open(file_path, "w", encoding='utf-8') as file:
file.write(content)