本文目录导读:

我来介绍几种批量转换Markdown文件的方法:
使用 Python + Pandoc(最推荐)
安装依赖
pip install pandoc pypandoc
转换脚本
批量转HTML:
import os
import pypandoc
from pathlib import Path
def batch_convert_md(input_dir, output_dir, output_format='html'):
"""批量转换Markdown文件"""
# 创建输出目录
Path(output_dir).mkdir(parents=True, exist_ok=True)
# 遍历所有md文件
for md_file in Path(input_dir).glob('*.md'):
try:
# 生成输出文件名
output_file = Path(output_dir) / f"{md_file.stem}.{output_format}"
# 转换
pypandoc.convert_file(
str(md_file),
output_format,
outputfile=str(output_file)
)
print(f"✓ 已转换: {md_file.name} → {output_file.name}")
except Exception as e:
print(f"✗ 转换失败 {md_file.name}: {e}")
# 使用示例
batch_convert_md('./markdown_files', './output_html', 'html')
批量转PDF:
def convert_to_pdf(input_dir, output_dir):
"""批量转换为PDF"""
for md_file in Path(input_dir).glob('*.md'):
output_file = Path(output_dir) / f"{md_file.stem}.pdf"
pypandoc.convert_file(
str(md_file),
'pdf',
outputfile=str(output_file),
extra_args=['--pdf-engine=xelatex'] # 可选,用于中文支持
)
使用 Bash 脚本 (Linux/Mac)
#!/bin/bash
# 批量转HTML
for file in *.md; do
if [ -f "$file" ]; then
filename="${file%.*}"
pandoc "$file" -o "${filename}.html"
echo "已转换: $file → ${filename}.html"
fi
done
# 批量转PDF
for file in *.md; do
if [ -f "$file" ]; then
filename="${file%.*}"
pandoc "$file" -o "${filename}.pdf" --pdf-engine=xelatex
fi
done
# 递归处理子目录
find . -name "*.md" -type f | while read file; do
filename=$(basename "$file" .md)
pandoc "$file" -o "${filename}.docx"
done
使用 PowerShell (Windows)
# 批量转HTML
Get-ChildItem -Path . -Filter *.md | ForEach-Object {
$outputName = $_.BaseName + ".html"
pandoc $_.FullName -o $outputName
Write-Host "已转换: $($_.Name) → $outputName"
}
# 批量转多种格式
$formats = @("html", "pdf", "docx")
Get-ChildItem -Path . -Recurse -Filter *.md | ForEach-Object {
foreach ($format in $formats) {
$outputName = $_.BaseName + ".$format"
pandoc $_.FullName -o $outputName
}
}
使用 Node.js
// 安装依赖: npm install marked fs-extra
const marked = require('marked');
const fs = require('fs-extra');
const path = require('path');
async function batchConvert(inputDir, outputDir) {
await fs.ensureDir(outputDir);
const files = await fs.readdir(inputDir);
const mdFiles = files.filter(f => f.endsWith('.md'));
for (const file of mdFiles) {
const content = await fs.readFile(path.join(inputDir, file), 'utf-8');
const html = marked.parse(content);
const outputFile = file.replace('.md', '.html');
await fs.writeFile(path.join(outputDir, outputFile), html);
console.log(`✓ 已转换: ${file}`);
}
}
batchConvert('./md', './html');
支持多种格式的完整脚本
import os
import pypandoc
from pathlib import Path
import shutil
class MarkdownConverter:
def __init__(self, input_dir, output_dir):
self.input_dir = Path(input_dir)
self.output_dir = Path(output_dir)
def convert_all(self, formats=['html', 'pdf', 'docx', 'txt', 'epub']):
"""批量转换为多种格式"""
for md_file in self.input_dir.glob('*.md'):
for fmt in formats:
self._convert_file(md_file, fmt)
def _convert_file(self, md_file, output_format):
"""转换单个文件"""
output_file = self.output_dir / f"{md_file.stem}.{output_format}"
# 特殊处理PDF
extra_args = []
if output_format == 'pdf':
extra_args = ['--pdf-engine=xelatex']
try:
pypandoc.convert_file(
str(md_file),
output_format,
outputfile=str(output_file),
extra_args=extra_args
)
print(f"✓ {md_file.name} → {output_file.name}")
except Exception as e:
print(f"✗ 转换失败 {md_file.name} 到 {output_format}: {e}")
# 使用
converter = MarkdownConverter('./input', './output')
converter.convert_all(['html', 'docx'])
高级功能
递归处理子目录
def recursive_convert(root_dir, output_dir):
"""递归处理所有子目录"""
for md_file in Path(root_dir).rglob('*.md'):
relative_path = md_file.relative_to(root_dir)
output_path = Path(output_dir) / relative_path.with_suffix('.html')
output_path.parent.mkdir(parents=True, exist_ok=True)
pypandoc.convert_file(str(md_file), 'html', outputfile=str(output_path))
带进度条
from tqdm import tqdm
def convert_with_progress(input_dir, output_dir, output_format='html'):
files = list(Path(input_dir).glob('*.md'))
for md_file in tqdm(files, desc="转换进度"):
output_file = Path(output_dir) / f"{md_file.stem}.{output_format}"
pypandoc.convert_file(str(md_file), output_format, outputfile=str(output_file))
安装依赖
# Pandoc (必须) sudo apt install pandoc # Linux brew install pandoc # Mac # Windows: 下载安装包 # Python相关 pip install pypandoc tqdm # Node.js npm install marked fs-extra
选择最适合你的方法,最推荐使用 Python + Pandoc,兼容性好且功能强大。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播