我已经安装了mammoth.js模块,可将docx转换为html。我可以将其与单个文件一起使用。
如何对特定文件夹中的所有文件使用相同的模块?我正在尝试保存输出html文件,同时保留原始名称(当然不是扩展名)。可能我需要其他一些软件包...下面的代码用于所需目录中的单个文件:
var mammoth = require("mammoth");
mammoth.convertToHtml({path: "input/first.docx"}).then(function (resultObject) {
console.log('mammoth result', resultObject.value);
});
系统是win64
这样的事情应该工作
const fs = require('fs')
const path = require('path')
const mammoth = require('mammoth')
fs.readdir('input/', (err, files) => {
files.forEach(file => {
if (path.extname(file) === '.docx') {
// If its a docx file
mammoth
.convertToHtml({ path: `input/${file}` })
.then(function(resultObject) {
// Now get the basename of the filename
const filename = path.basename(file)
// Replace output/ with where you want the file
fs.writeFile(`output/${filename}.html`, resultObject.value, function (err) {
if (err) return console.log(err);
});
})
}
})
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句