我已经安装了将docx转换为html的mammoth.js模块。我可以在一个文件中使用它。
如何对特定文件夹中的所有文件使用相同的模块?我正在尝试保存输出的html文件,同时保留原始名称(当然不是扩展名)。也许我需要一些其他的包。以下代码用于所需目录中的单个文件:
var mammoth = require("mammoth");
mammoth.convertToHtml({path: "input/first.docx"}).then(function (resultObject) {
console.log('mammoth result', resultObject.value);
});系统为win64
发布于 2020-06-05 21:57:19
像这样的东西应该是可行的
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);
});
})
}
})
})https://stackoverflow.com/questions/62216455
复制相似问题