Nodejs 啦啦啦,求一个关于word的模块

Nodejs 啦啦啦,求一个关于word的模块

想找一个能够导入和导出word的模块,github上找到了一个DocxTemplater 貌似不给力,不知道前辈们 有没有什么其他的模块 推荐一下

3 回复

当然可以!如果你正在寻找一个能够导入和导出Word文档的Node.js模块,这里有几个备选方案。这些模块可以帮助你更高效地处理Word文档。以下是一些推荐的模块及其使用示例。

1. mammoth

mammoth 是一个用于将Word文档(.docx)转换为HTML的工具。虽然它不能直接导出Word文档,但它非常适合读取和解析Word文档的内容。

示例代码:

const mammoth = require("mammoth");

mammoth.convertToHtml({path: "./example.docx"})
  .then(function(result){
    console.log(result.value); // HTML 输出
    result.messages.forEach(function(message) {
      console.log(message);
    });
  })
  .catch(function(error) {
    console.error("Error:", error);
  });

2. docx

docx 是一个强大的库,允许你创建、修改和导出Word文档(.docx)。你可以用它来生成复杂的Word文档,包括段落、表格、图片等。

示例代码:

const { Document, Packer, Paragraph } = require('docx');

const doc = new Document({
    sections: [
        {
            properties: {},
            children: [
                new Paragraph({
                    text: "Hello World!",
                    color: "0000FF",
                }),
            ],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

3. officegen

officegen 是另一个处理Office文档(包括Word)的强大工具。它可以用来生成Word文档并进行基本操作。

示例代码:

const officegen = require('officegen');
const fs = require('fs');

// 创建一个新的Word文档
let docx = officegen({
    type: 'docx'
});

// 添加一段文本
docx.createP().addText('Hello World!');

// 将生成的文档写入文件
let out = fs.createWriteStream('./hello.docx');
out.on('close', function () {
    console.log('Done!');
});

docx.generate(out);

希望这些示例对你有帮助!你可以根据你的具体需求选择合适的模块。如果需要进一步的帮助或有其他问题,请随时告诉我。


操作word并不难,nodejs可以支持activeXObject对了。 有个 win32com 模块 win32com\node_modules\win32ole\examples\word_sample.js 代码贴在下面 var win32ole = require(‘win32ole’); win32ole.print(‘word_sample\n’); console.log(win32ole.version()); var path = require(‘path’); var cwd = path.join(win32ole.MODULEDIRNAME, ‘…’);

var fs = require(‘fs’); var tmpdir = path.join(cwd, ‘test/tmp’); if(!fs.existsSync(tmpdir)) fs.mkdirSync(tmpdir); var outfile = path.join(tmpdir, ‘word_sample.doc’);

var word_sample = function(filename){ var wd = win32ole.client.Dispatch(‘Word.Application’); wd.Visible = true; var doc = wd.Documents.Add(); var para = doc.Content.Paragraphs.Add(); para.Range.Text = ‘stringUTF8’; try{ console.log(‘saving to: "’ + filename + ‘" …’); var result = doc.SaveAs(filename); console.log(result); // *** undefined }catch(e){ console.log(’(exception cached)\n’ + e); } wd.Documents.Close(); wd.Quit(); };

try{ word_sample(outfile); }catch(e){ console.log(’*** exception cached ***\n’ + e); }

对于需要处理 Word 文档的 Node.js 项目,可以使用 mammothdocx 这样的库来实现导入和导出功能。下面分别介绍这两个库,并提供一些简单的示例代码。

mammoth

mammoth 主要用于将 Word 文档(.docx)转换为 HTML 和 Markdown 格式,非常适合需要读取 Word 文档内容的场景。

安装

npm install mammoth

示例代码

const mammoth = require("mammoth");

async function convertDocxToHtml() {
    const result = await mammoth.convertToHtml({ path: "example.docx" });
    const html = result.value; // The generated HTML
    const messages = result.messages; // Any messages, such as warnings during conversion
    console.log(html);
}

convertDocxToHtml();

docx

如果你需要创建或修改 Word 文档,可以考虑使用 docx 库。此库允许你从头开始构建文档或者修改现有的 .docx 文件。

安装

npm install docx

示例代码

const { Document, Packer, Paragraph } = require("docx");

// 创建一个新的 Word 文档
const doc = new Document({
    sections: [
        {
            properties: {},
            children: [
                new Paragraph({
                    text: "Hello World",
                }),
            ],
        },
    ],
});

// 将文档保存到文件中
Packer.toBuffer(doc).then((buffer) => {
    require("fs").writeFile(`hello-world.docx`, buffer, (err) => {
        if (err) throw err;
        console.log(`Document created successfully`);
    });
});

综上所述,根据你的具体需求选择合适的库。如果主要是为了读取 Word 内容,mammoth 可能更适合;如果需要创建或编辑 Word 文档,则推荐使用 docx。希望这些建议对你有所帮助!

回到顶部