使用nodejs输出著作权申请所需的word版源码

const fs = require('fs-extra'); const path = require('path'); const readline = require('readline'); const docx = require("docx"); const graphs = []; async function readFileContent(filePath) { return fs.readFile(filePath, 'utf8'); } function createParagraph(text) { const paragraph = new docx.Paragraph({ children: [new docx.TextRun(text)] }); graphs.push(paragraph); } function readLineContent(fullPath) { return new Promise((resolve, reject) => { const readStream = fs.createReadStream(fullPath); const rl = readline.createInterface({ input: readStream, crlfDelay: Infinity }); rl.on('line', (line) => { // console.log('Line from file:', line); createParagraph(line); }); rl.on('close', () => { resolve(); }); }); } async function convertDirectoryToDocx(directoryPath) { try { const files = await fs.readdir(directoryPath, { withFileTypes: true }); for (const file of files) { const fullPath = path.join(directoryPath, file.name); if (file.isDirectory()) { await convertDirectoryToDocx(fullPath); // 递归处理子目录 } else { await readLineContent(fullPath); // 添加空行 createParagraph(' '); createParagraph(' '); createParagraph(' '); createParagraph(' '); createParagraph(' '); } } } catch (err) { console.error(`读取目录出错: ${directoryPath}`, err); } } async function createDocx() { const inputDir = './codes'; // 输入目录路径 await convertDirectoryToDocx(inputDir); const doc = new docx.Document({ sections: [ { properties: {}, children: graphs, }, ], }); const outputDocx = './merged_files.docx'; // 输出的.docx文件路径 docx.Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync(outputDocx, buffer); console.log(`文件已合并至: ${outputDocx}`); }); } createDocx();
上一篇:【IO】文件操作-2. 文件系统操作


下一篇:如何压缩视频大小,怎么压缩视频