将 PDF、DOC、DOCX、PPTX 办公文档转换为 Markdown,支持旧版 .doc 文件的文本提取和基本格式保留。
办公室到 Markdown 转换器技能 (v2).是一项面向实际任务的技能,主要用于Description.;Convert 办公文档( PDF, DOC, DOCX, PPTX) 到 Markdown 格式. 此技能使用字词提取库为.doc 支持并提供。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;
涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
将 Office 文档(PDF、DOC、DOCX、PPTX)转换为 Markdown 格式。该技能使用 word-extractor 库支持 .doc 格式,并提供完整的 OpenClaw 集成能力。
cp -r /root/.openclaw/workspace/office-to-md-v2/office-to-md /path/to/your/workspace/
cd /path/to/your/workspace/office-to-md
npm install
pip3 install python-pptx
// 转换任意受支持的文档
const result = await exec(
'node /path/to/office-to-md/openclaw-skill.js /path/to/document.doc',
{ workdir: '/path/to/workspace', timeout: 60000 }
);
if (result.exitCode === 0) {
console.log('✅ 文档转换成功');
// 输出文件路径:/path/to/document.md
} else {
console.error('❌ 转换失败:', result.stderr);
}
// 导入转换器
const { convertOfficeToMarkdown } = require('/path/to/office-to-md/openclaw-skill.js');
// 转换文档
const conversionResult = await convertOfficeToMarkdown('/path/to/document.pdf');
if (conversionResult.success) {
console.log(`输出路径:${conversionResult.outputPath}`);
console.log(`预览内容:${conversionResult.preview}`);
} else {
console.error(`错误信息:${conversionResult.error}`);
}
async function convertDocumentToMarkdown(filePath) {
// 验证文件是否存在
try {
await read(filePath);
} catch (error) {
return { success: false, error: `文件未找到:${filePath}` };
}
// 检查文件扩展名
const ext = filePath.toLowerCase().slice(-5);
const supported = ['.pdf', '.doc', '.docx', '.pptx'];
if (!supported.some(s => ext.endsWith(s))) {
return {
success: false,
error: `不支持的文件类型。支持的格式:${supported.join(', ')}`
};
}
// 使用技能执行转换
const cmd = `node /path/to/office-to-md/openclaw-skill.js "${filePath}"`;
const result = await exec(cmd, {
workdir: '/path/to/workspace',
timeout: 120000 // 大型文件最多耗时 2 分钟
});
if (result.exitCode === 0) {
const outputPath = filePath.replace(/.[^/.]+$/, '.md');
return {
success: true,
outputPath: outputPath,
message: `已转换为:${outputPath}`
};
} else {
return {
success: false,
error: result.stderr || '转换失败'
};
}
}
// 使用示例
const result = await convertDocumentToMarkdown('/path/to/document.doc');
if (result.success) {
const markdown = await read(result.outputPath);
console.log(markdown.substring(0, 1000));
}
// 转换 .doc 文件并分析其内容
const docPath = '/path/to/document.doc';
const convertResult = await exec(
`node /path/to/office-to-md/openclaw-skill.js "${docPath}"`,
{ workdir: '/path/to/workspace' }
);
if (convertResult.exitCode === 0) {
const mdPath = docPath.replace('.doc', '.md');
const content = await read(mdPath);
// 分析内容
const wordCount = content.split(/s+/).length;
const lines = content.split('n').length;
const hasChinese = /[u4e00-u9fff]/.test(content);
console.log(`文档分析结果:`);
console.log(`- 词数:${wordCount}`);
console.log(`- 行数:${lines}`);
console.log(`- 是否含中文:${hasChinese}`);
console.log(`- 预览(前 200 字符):${content.substring(0, 200)}...`);
}
// 批量转换多种格式的文档
const documents = [
'/path/to/report.pdf',
'/path/to/legacy.doc',
'/path/to/modern.docx',
'/path/to/presentation.pptx'
];
const results = [];
for (const doc of documents) {
console.log(`正在转换 ${doc}...`);
const result = await exec(
`node /path/to/office-to-md/openclaw-skill.js "${doc}"`,
{ workdir: '/path/to/workspace', timeout: 90000 }
);
const success = result.exitCode === 0;
results.push({
file: doc,
success: success,
error: success ? null : result.stderr
});
console.log(success ? '✅ 成功' : '❌ 失败');
}
// 汇总统计
const successful = results.filter(r => r.success).length;
console.log(`n转换汇总:${successful}/${results.length} 个成功`);
返回一个 Promise,其解析值为:
{
success: boolean,
outputPath?: string,
markdown?: string,
preview?: string,
fileType?: string,
message?: string,
stats?: {
lines: number,
characters: number,
words: number
},
error?: string,
stack?: string
}
node --max-old-space-size=4096 openclaw-skill.js large-file.doc
“文件未找到”
“不支持的文件类型”
.doc 文件转换出错
中文显示为乱码
超时错误
通过设置环境变量启用调试日志:
DEBUG=office-to-md node openclaw-skill.js document.doc
本技能按“原样”提供。所依赖的底层库各自拥有独立许可证: