Ob 链接替换


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

# 定义顶级文件夹路径
folder_path = r'D:\Obsidian\Obsidian\2. Data\2.3 Item'

# 遍历文件夹及其所有子文件夹中的 .md 文件
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith('.md'):
file_path = os.path.join(root, filename)

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

# 使用正则表达式查找并替换图片引用
pattern = r'!\[\[Pasted image (\d{14}\.png)\]\]'
replacement = r'![Pasted image \1](file:///D:%5CObsidian%5CObsidian_ATT%5CCloth%5CPasted%20image%20\1)'
new_content = re.sub(pattern, replacement, content)

# 检查是否有改动,避免不必要的写入
if content != new_content:
# 写入修改后的内容到原文件
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
print(f"文件 {file_path} 已修改。")
else:
print(f"文件 {file_path} 无需修改。")

print("所有文件及其子文件夹中的文件已处理完成。")


Ob 属性添加


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


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

# 处理文件内容,确保 cover 属性值在 YAML 前导中不会因为特殊字符(如换行符)引起问题
cover_content = content.replace('\n', '\\n') # 用 `\\n` 替换换行符

# 检查文件是否已有 YAML 前导
yaml_header_match = re.match(r'^---\n(.*?)\n---\n(.*)', content, re.DOTALL)

if yaml_header_match:
yaml_header, body = yaml_header_match.groups()
# 添加或更新 cover 属性
if not re.search(r'^cover:', yaml_header, re.MULTILINE):
yaml_header += f'\ncover: "{cover_content}"\n'
else:
yaml_header = re.sub(r'^cover:.*$', f'cover: "{cover_content}"', yaml_header, flags=re.MULTILINE)
new_content = f'---\n{yaml_header}\n---\n{body}'
else:
# 没有 YAML 前导的情况
new_content = f'---\ncover: "{cover_content}"\n---\n{content}'

# 写入更新后的内容到文件
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)

print(f'Processed {file_path}')


def process_markdown_files(root_dir):
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith('.md'):
file_path = os.path.join(dirpath, filename)
add_cover_property(file_path)


# 替换为实际的目录路径
root_directory = r'自定义目录'
process_markdown_files(root_directory)


Ob 特定内容添加到属性


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

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

# 提取 `![[Pasted image ***.png]]` 的内容
cover_match = re.search(r'!\[\[Pasted image \d+\.png\]\]', content)
if cover_match:
cover_content = cover_match.group(0)
else:
print(f"No matching 'Pasted image' found in {file_path}")
return

# 检查文件是否已有 YAML 前导
yaml_header_match = re.match(r'^---\n(.*?)\n---\n(.*)', content, re.DOTALL)

if yaml_header_match:
yaml_header, body = yaml_header_match.groups()
# 添加或更新 cover 属性
if not re.search(r'^cover:', yaml_header, re.MULTILINE):
yaml_header += f'\ncover: "{cover_content}"\n'
else:
yaml_header = re.sub(r'^cover:.*$', f'cover: "{cover_content}"', yaml_header, flags=re.MULTILINE)
new_content = f'---\n{yaml_header}\n---\n{body}'
else:
# 没有 YAML 前导的情况
new_content = f'---\ncover: "{cover_content}"\n---\n{content}'

# 写入更新后的内容到文件
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)

print(f'Processed {file_path}')

def process_markdown_files(root_dir):
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith('.md'):
file_path = os.path.join(dirpath, filename)
add_cover_property(file_path)

# 替换为实际的目录路径
root_directory = r'自定义目录'
process_markdown_files(root_directory)