Spaces:
Running
Running
import os | |
import re | |
import subprocess | |
def convert_md_files_to_png(md_file_path, output_base_dir="./Marp/"): | |
""" | |
顺序读取和输入与 md_file_path 文件相同路径中的所有命名格式为 {base_name}_{index}.md 的文件, | |
并按 index 的数字大小顺序遍历所有文件,使用 Marp 直接生成 PNG 格式的图片。 | |
:param md_file_path: MD 文件的完整路径。 | |
:param output_base_dir: 输出目录的基路径,将在此目录下创建子目录以保存输出文件。 | |
""" | |
if not os.path.exists(output_base_dir): | |
os.makedirs(output_base_dir) | |
# 获取MD文件名(去掉.md后缀) | |
base_name = os.path.splitext(os.path.basename(md_file_path))[0] | |
directory = os.path.dirname(md_file_path) | |
# 获取所有符合条件的文件名 | |
md_files = [f for f in os.listdir(directory) if f.startswith(base_name + '_') and f.endswith('.md')] | |
md_files.sort() # 按文件名排序 | |
# 创建输出目录,目录名与MD文件名相同 | |
output_dir = os.path.join(output_base_dir, base_name) | |
os.makedirs(output_dir, exist_ok=True) # 如果目录已存在,则不会抛出异常 | |
for md_file in md_files: | |
md_file_path = os.path.join(directory, md_file) | |
base = os.path.splitext(os.path.basename(md_file_path))[0] | |
match = re.match(rf"{re.escape(base_name)}_(\d+)", base) | |
if match: | |
index = int(match.group(1)) # 提取索引 | |
# 构建Marp CLI命令 | |
command = ["marp", "--html", "--allow-local-files", | |
"--output", os.path.join(output_dir, f"{base_name}_{index}.png"), | |
"--format", "png", | |
md_file_path] | |
try: | |
# 执行命令,将MD文件转换为PNG | |
subprocess.run(command, check=True) | |
print(f"成功将 '{md_file_path}' 转换为PNG并保存至 '{output_dir}'。") | |
except subprocess.CalledProcessError as e: | |
print(f"转换 '{md_file_path}' 时发生错误: {e}") |