首页 > 解决方案 > 如何在nodeJS中将.doc文件读取/重写为xml?

问题描述

我需要阅读 .doc 文件,更改一些属性并保存它。我怎么能做到这一点?

我可以阅读 .docx 文件,如下所示:

const zip = new AdmZip(filePath);
const xml = zip.readAsText('word/document.xml');
console.log(xml)

//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
//<w:document //xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessi//ngCanvas" //xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" //xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chart//ex...

我尝试像这样阅读.doc:

const expectedXml = fs.readFileSync(filePath);

但我得到了不分青红皂白的结果。

我希望得到像.docx 示例中那样的xml。

标签: node.jsxmlparsingdocxdoc

解决方案


Microsoft DOC文件早于DOCX,并且不基于压缩 (OPC) XML (OOXML);它们是二进制文件格式

对于一次性,在 MS Word 或 LibreOffice 中打开 DOC 文件并重新保存为 DOCX。

要在 NodeJS 中以编程方式提取文本,请使用textract 之类的包。


推荐阅读