季布, 2023-03-16 12:36 采纳率: 35.1%
浏览 199
已结题

python docx 把数据写入表格



 
course_link = [[{'course': '课程1', 'link': 'www.souhu.com', 'info': '加强计算机学习,学习计划??'}, {'course': '课程2', 'link': '', 'info': '加强计算机学习,学习计划??'}], [{'course': '111', 'link': 'www.111.com', 'info': '人工智能,sadsaads'}]]
 
 
 
school_major_link = [['Massachusetts Institute of Technology (MIT)  一 计算机科学与技术 一 www.baidu.com', 'Massachusetts Institute of Technology (MIT)  一 金融学 一 '], ['Imperial College London 一 软件工程 一 www.google.com']]

需要生成表格样式的docx文档,

我这只完成了一部分,实在搞不懂怎么写了

img

course_link 和 school_major_link 是一一对应的
比如把'Massachusetts Institute of Technology (MIT) 一 计算机科学与技术 一 http://www.baidu.com%27/ 这个拆开分别填写到对应的位置,那[{'course': '课程1', 'link': 'http://www.souhu.com%27/, 'info': '加强计算机学习,学习计划??'}, {'course': '课程2', 'link': '', 'info': '加强计算机学习,学习计划??'}] 这个填写到

img

在生成一行 把'Massachusetts Institute of Technology (MIT) 一 金融学 一 ' 这个拆开 填写进去 在写入[{'course': '课程1', 'link': 'http://www.souhu.com%27/, 'info': '加强计算机学习,学习计划??'}, {'course': '课程2', 'link': '', 'info': '加强计算机学习,学习计划??'}],以此类推

有疑问的可以说出来

补充:
其实原始数据是:

intention = {'specialist': [{'specialist': '什么?', 'selectSchool': [
    'Massachusetts Institute of Technology (MIT)  一 计算机科学与技术 一 www.baidu.com',
    'Massachusetts Institute of Technology (MIT)  一 金融学 一 '], 'checked1': True}, {'specialist': '具体是什么?',
                                                                                       'selectSchool': [
                                                                                           'Imperial College London 一 软件工程 一 www.google.com'],
                                                                                       }]}

career = {'firstcareerspecialist': [
    {'plan': '学习计划??', 'firstcareerspecialist': '加强计算机学习',
     'jobs': '岗位', 'company': '公司\n', 'company1': '公司1', 'target': '目标',
     'CourseLink': [{'course': '课程1', 'link': 'www.souhu.com'}, {'course': '课程2', 'link': ''}]},
    {'plan': 'sadsaads',
     'CourseLink': [{'course': '111', 'link': 'www.111.com'}],
     }]}


其实总共应该再表格中显示三行
第一行
比如 intention 中的 selectSchool 中的 Massachusetts Institute of Technology (MIT)  一 计算机科学与技术 一 www.baidu.com'  和   'CourseLink': [{'course': '课程1', 'link': 'www.souhu.com'}, {'course': '课程2', 'link': ''}] 这是一行数据 并且将 career中的plan 和 target拼接在一起显示

第二行
intention 中的 selectSchool 中的  'Massachusetts Institute of Technology (MIT)  一 金融学 一 '  和   'CourseLink': [{'course': '课程1', 'link': 'www.souhu.com'}, {'course': '课程2', 'link': ''}] 这是一行数据 并且将 career中的plan 和 target拼接在一起显示

第三行
intention 中的 selectSchool 中的   'Imperial College London 一 软件工程 一 www.google.com' 和   'CourseLink': [{'course': '111', 'link': 'www.111.com'}],这是一行数据 并且将 career中的plan 和 target拼接在一起显示

需要将intention 中的每一个以 一 分割 分割后是 学校专业 链接

img

career中的plan 和 target拼接在一起显示再

img

  • 写回答

9条回答 默认 最新

  • 眉山央央an 2023-03-20 03:55
    关注

    哥哥,你可以使用 docxtemplaterxlsx-populate 这两个库来生成表格样式的 docx 文档。首先,你需要安装这两个库:

    npm install docxtemplater xlsx-populate
    

    然后,你可以使用以下代码来生成表格:

    const XlsxPopulate = require('xlsx-populate');
    const Docxtemplater = require('docxtemplater');
    const fs = require('fs');
    
    // 数据
    const intention = {
      'specialist': [
        {
          'specialist': '什么?',
          'selectSchool': [
            'Massachusetts Institute of Technology (MIT)  一 计算机科学与技术 一 www.baidu.com',
            'Massachusetts Institute of Technology (MIT)  一 金融学 一 '
          ],
          'checked1': true
        },
        {
          'specialist': '具体是什么?',
          'selectSchool': [
            'Imperial College London 一 软件工程 一 www.google.com'
          ]
        }
      ]
    };
    
    const career = {
      'firstcareerspecialist': [
        {
          'plan': '学习计划??',
          'firstcareerspecialist': '加强计算机学习',
          'jobs': '岗位',
          'company': '公司\n',
          'company1': '公司1',
          'target': '目标',
          'CourseLink': [
            {'course': '课程1', 'link': 'www.souhu.com'},
            {'course': '课程2', 'link': ''}
          ]
        },
        {
          'plan': 'sadsaads',
          'CourseLink': [
            {'course': '111', 'link': 'www.111.com'}
          ]
        }
      ]
    };
    
    // 处理数据并生成文档
    async function generateDocx() {
      // 处理数据
      const data = [];
      for (let i = 0; i < intention.specialist.length; i++) {
        const schools = intention.specialist[i].selectSchool;
        const courseLinks = career.firstcareerspecialist[i].CourseLink;
    
        for (let j = 0; j < schools.length && j < courseLinks.length; j++) {
          const [school, major, link] = schools[j].split(' 一 ');
          const { course, link: courseLink } = courseLinks[j];
          const info = `${career.firstcareerspecialist[i].plan} ${career.firstcareerspecialist[i].target}`;
    
          data.push({
            school,
            major,
            link,
            course,
            courseLink,
            info
          });
        }
      }
    
      // 读取模板文件
      const content = fs.readFileSync('template.docx', 'binary');
    
      // 使用 Docxtemplater 填充模板
      const doc = new Docxtemplater();
      doc.loadZip(content);
      doc.setData({ data });
      doc.render();
    
      // 保存文档
      const buffer = doc.getZip().generate({ type: 'nodebuffer' });
      fs.writeFileSync('output.docx', buffer);
    }
    
    generateDocx();
    

    在上面的代码中,我们首先将数据处理成表格需要的格式,然后读取一个名为 template.docx 的 Word 模板文件,并使用 docxtemplater 填充模板中的变量。最后,将填充好的文档保存到名为 output.docx 的文件中。

    • 注意,在上述代码中,我们没有直接使用 xlsx-populate 来生成表格,而是将数据转换成了 docx 文件格式,并使用 docxtemplater 和 Word 模板来生成表格。如果你希望使用 xlsx-populate 直接生成表格,也是可以的。你可以参考以下代码:
    const XlsxPopulate = require('xlsx-populate');
    const fs = require('fs');
    
    // 数据
    const intention = {
      'specialist': [
        {
          'specialist': '什么?',
          'selectSchool': [
            'Massachusetts Institute of Technology (MIT)  一 计算机科学与技术 一 www.baidu.com',
            'Massachusetts Institute of Technology (MIT)  一 金融学 一 '
          ],
          'checked1': true
        },
        {
          'specialist':
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月22日
  • 修改了问题 3月16日
  • 赞助了问题酬金15元 3月16日
  • 创建了问题 3月16日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果