首页 > 解决方案 > 将数据导出到文件 docx、node.js 和 react js

问题描述

我使用node.Js和react Js,我想将数据放入docx文件中,就像图片中一样,我有一个docxtemplater参考,但它不是免费的,请有人帮帮我

在此处输入图像描述

标签: javascriptnode.jsreactjs

解决方案


你可以在 Python 中做到这一点。创建一个 Python 脚本并从 NodeJS 调用它。Python 有一些用于docx操作的库。您可以使用的是python-docx-template.

步骤1:

创建一个适合您的 Python 脚本。

第2步

您可以使用spawn模块从 NodeJS 调用 Python 脚本。

const { spawn } = require('child_process');

const python_script = spawn('python', ['relative_path_to_python_script']);
python_script.stdin.write('You can send some inputs to Python script here');
python_script.stdin.end();
python_script.stdout.on('data', (data) => {
  // data - output from Python script
  // This will execute after the Python script finishes
});
python_script.stderr.on('data', (error) => {
  // This will execute if Python script throws an error
});

推荐阅读