node.js - 如何在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。
解决方案
Microsoft DOC文件早于DOCX,并且不基于压缩 (OPC) XML (OOXML);它们是二进制文件格式。
对于一次性,在 MS Word 或 LibreOffice 中打开 DOC 文件并重新保存为 DOCX。
要在 NodeJS 中以编程方式提取文本,请使用textract 之类的包。
推荐阅读
- react-native - MQTT vs MQTT over websocket 反应原生
- php - 一个月的几个星期
- python - 印尼笑的正则表达式模式 (wkwkwk)
- python - 如何为图像中的对象分别用不同的颜色着色
- node.js - 尝试通过 axios 发送带有另一个参数的 formData
- javascript - 如何控制台.log 下拉菜单中的属性?
- html - Html 视频背景不适用于 Ionic 框架上的 iOS
- kotlin-coroutines - 单元测试 Kotlin Flows 和 toList()
- javascript - 如何修复内容安全策略错误节点应用程序
- rust - 在 Rust 中将成员推入 Vec 的 Reducer