odg-processor
v1.1.1
Published
A Node.js package for processing ODG (OpenDocument Graphics) files using LibreOffice API
Maintainers
Readme
ODG Processor
一个用于处理 ODG(OpenDocument Graphics)文件的 Node.js 包,基于 LibreOffice / Python UNO 实现,支持读取文件信息、修改文本、按关键字搜索替换,并导出为 PDF 或 PNG。
特性
- 读取 ODG 文件信息:页面、形状、文本、文档属性
- 按形状名称批量修改文本
- 按关键字搜索并替换文本
- 导出 PDF
- 导出 PNG
- PNG 水印处理
- 自动检测 LibreOffice / OpenOffice 路径
- 默认只导出,不会保存回源 ODG
安装
npm install odg-processor环境要求
- Node.js >= 12
- 已安装 LibreOffice 或 OpenOffice
- Python 3.x
- Pillow(仅 PNG 水印功能需要)
安装 Pillow:
pip install Pillow快速开始
读取文件信息
const { getODGInfo } = require('odg-processor');
async function main() {
const result = await getODGInfo('document.odg');
if (!result.success) {
console.error(result.error);
return;
}
console.log('文件名:', result.data.file_name);
console.log('页数:', result.data.pages_count);
for (const page of result.data.pages_info) {
for (const shape of page.shapes) {
console.log(shape.shape_name, shape.text || '');
}
}
}
main();修改文本并导出 PDF
const { modifyODGTexts } = require('odg-processor');
async function main() {
const result = await modifyODGTexts('template.odg', {
title: '新标题',
name: '张三',
salary: '8000',
}, {
outputPath: 'output',
exportFormat: 'pdf',
});
console.log(result);
}
main();搜索并替换
const { searchAndReplace } = require('odg-processor');
async function main() {
const result = await searchAndReplace('template.odg', {
'{{公司名}}': '某某科技有限公司',
'{{日期}}': '2026-04-23',
}, {
outputPath: 'output',
exportFormat: 'pdf',
});
console.log(result);
}
main();导出 PNG
const { exportToPNG } = require('odg-processor');
await exportToPNG('template.odg', 'output.png', {
pageNumber: 0,
resolution: 300,
});导出 PNG 并添加水印
const { exportToPNGWithWatermark } = require('odg-processor');
await exportToPNGWithWatermark('template.odg', 'output.png', {
pageNumber: 0,
resolution: 300,
watermarkText: '内部资料',
position: 'bottom-right',
opacity: 0.35,
});API 概览
便捷函数
getODGInfo(filePath, options)modifyODGTexts(filePath, shapeTextMap, options)modifyODGText(filePath, shapeName, newText, options)searchAndReplace(filePath, replacements, options)exportToPNG(filePath, outputPath, options)addWatermark(imagePath, options)exportToPNGWithWatermark(filePath, outputPath, options)applyAdvancedWatermark(imagePath, outputPath, patternType, options)
ODGProcessor
const { ODGProcessor } = require('odg-processor');
const processor = new ODGProcessor(options);可用方法:
getODGInfo(filePath)modifyTexts(filePath, shapeTextMap, options)modifyText(filePath, shapeName, newText, outputPath, exportPDF, saveODG)createODG(outputPath)exportToPDF(filePath, outputPath)exportToPNG(filePath, outputPath, pageNumber, resolution)searchAndReplace(filePath, replacements, options)addWatermark(imagePath, options)exportToPNGWithWatermark(filePath, outputPath, options)applyAdvancedWatermark(imagePath, outputPath, patternType, options)
选项说明
modifyODGTexts / modifyText / searchAndReplace
常用选项:
outputPath:输出路径。会作为导出文件的基础路径。exportFormat:'pdf' | 'png' | 'both' | 'none'resolution:PNG 分辨率,默认300pageNumber:PNG 页码,从0开始,null表示全部页面saveODG:是否保存修改后的 ODG,默认false
默认行为
- 默认不会保存源 ODG 文件
- 如果你只想导出 PDF / PNG,直接使用默认配置即可
- 只有显式传入
saveODG: true,并且输出路径是.odg时,才会保存修改后的 ODG
示例:
await modifyODGTexts('template.odg', {
name: '张三'
}, {
outputPath: 'result.odg',
exportFormat: 'pdf',
saveODG: true,
});如果不传 saveODG,则只会导出,不会写回源文件。
示例
只导出 PDF,不保存 ODG
await modifyODGTexts('template.odg', {
name: '张三'
}, {
outputPath: 'result',
exportFormat: 'pdf',
});同时导出 PDF 和 PNG
await modifyODGTexts('template.odg', {
name: '张三'
}, {
outputPath: 'result',
exportFormat: 'both',
resolution: 300,
});只保存 ODG
await modifyODGTexts('template.odg', {
name: '张三'
}, {
outputPath: 'result.odg',
exportFormat: 'none',
saveODG: true,
});LibreOffice 路径
程序会自动尝试查找 LibreOffice / OpenOffice 的 Python 运行环境。
如需手动指定:
const processor = new ODGProcessor({
pythonPath: '/path/to/libreoffice/python',
libreOfficePath: '/path/to/libreoffice/python',
});测试
运行项目测试:
npm test许可证
MIT
