npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

odg-processor

v1.1.1

Published

A Node.js package for processing ODG (OpenDocument Graphics) files using LibreOffice API

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 分辨率,默认 300
  • pageNumber: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