zhangyu836 / xltpl

A python module to generate xls/x files from a xls/x template.
MIT License
71 stars 17 forks source link

Excel模板配置时是否支持嵌套循环? #2

Closed bruthliu closed 4 years ago

bruthliu commented 4 years ago

Excel模板配置时是否支持嵌套循环与单元格合并处理呢 截图

bruthliu commented 4 years ago

截图

zhangyu836 commented 4 years ago

不确定你要解决的是什么问题。可以把文件、数据、代码发出来看一下。

zhangyu836 commented 4 years ago

附件在哪里?

zhangyu836 commented 4 years ago

可能不能用邮件来发送附件,会被过滤掉。

bruthliu commented 4 years ago

非常感谢收到您的回复: 我的excel模板是类似这样的。一码通代表一个人,可能通过多个渠道(渠道是固定的)进行交易,每个渠道交易可能存在有多个mac地址。 我要解决的问题是对人和mac根据渠道进行模板数据填充: 对一码通使用了for item in rows ,同时又对mac使用使用for 循环,总是报错。 附件是我测试的示例和模板文件。请指导,谢谢。 附件2:持续多日被阻断一码通统计表MM月模板.xlsx

bruthliu commented 4 years ago

-- coding: utf-8 --

import os from datetime import datetime from xltpl.writerx import BookWriter

def write_test(intemplatepath,payloads,outputexcelpath): writer = BookWriter(intemplatepath) writer.jinja_env.globals.update(dir=dir, getattr=getattr) writer.render_book2(payloads=payloads) writer.save(outputexcelpath)

now = datetime.now() t2_data_dict = { 'value_1800909831':1800909831, 'value_1800909831':1800909831, 'date':now }

rows = [['180090983105'],

     # ['190001298552']]

rows = [['1800909831',[['D017C2870C24'],['3C77E6F32EF4'],['5056856397']]], ['1900012985',[['D017C2870C24'],['3C77E6F32EF4'],['5056856397']]]]

[['一码通'],[macs]]

tdxmacs = [

    # ['D017C2870C24'],
    # ['3C77E6F32EF4'],
    # ['5056856397']
    # ]

t2_data_dict['rows'] = rows

t2_data_dict['tdxmacs'] = tdxmacs

payload_t2 = {'tpl_name': '表2', 'sheet_name': u'表2', 'ctx': t2_data_dict}

payload_a5 = {'tpl_idx': 2, 'ctx': a5_data_dict}

payloads = [payload_t2]

if name == "main": datadate='20200529' intemplatepath =r'E:\附件2:持续多日被阻断一码通统计表MM月模板.xlsx' outputexcelpath = r'E:\附件2:持续多日被阻断一码通统计表MM月模板'+datadate+'.xlsx' write_test(intemplatepath,payloads,outputexcelpath)

bruthliu commented 4 years ago

好像不能直接上传py文件

zhangyu836 commented 4 years ago

mm.xlsx

你的情形和示例中的其实是一样的。 加第一列是为了可以找到第一个未合并单元格,在其中插入批注。 if else 的作用是触发单元格合并。 两行的格式相同时也是如此处理。 多行格式不同时可以按示例中的方法处理。 如果要按渠道循环,也需要类似的处理。

bruthliu commented 4 years ago

error mm.xlsx

按您说的,我尝试了一下,结果还是报错,麻烦再看一下,非常感谢!

zhangyu836 commented 4 years ago

或许是你使用的 openpyxl 有问题,重新安装一下

bruthliu commented 4 years ago

谢谢,问题已解决。openpyxl由2.5.12改成了3.0.3已解决问题。