liuzg50505 / pyexceltemplate

一个方便强大的excel模板引擎,基于python3!
BSD 3-Clause "New" or "Revised" License
9 stars 1 forks source link

我想就进行模板替换,怎么操作呢? #1

Open zongqiqi opened 6 years ago

zongqiqi commented 6 years ago

我有一些excel的数据,现在提取到list里面,能不能键值对的形式替换替换其他excel文件里面的内容,例如python-docx template.

liuzg50505 commented 6 years ago

可以按照testcase4中的代码这样,准备一个上下文dict,然后设置一个key='students' students里面是一个对象数组,每个元素表示一个学生的信息。

        processor = OpenpyXlExcelProcessor('xlstemplates/template4.xlsx', 'Sheet1')
        evaluator = PyEvaluator()
        embeddedEvaluator = EmbeddedPyEvaluator(evaluator)
        context = DictExcelTemplateContext({
            "students":[
                {'name':'s1', 'age':12, 'class':'2-A' ,'grade':'A-'},
                {'name':'s2', 'age':13, 'class':'2-B' ,'grade':'B-'},
                {'name':'s3', 'age':14, 'class':'2-C' ,'grade':'C-'},
                {'name':'s4', 'age':15, 'class':'2-D' ,'grade':'E-'},
                {'name':'s5', 'age':16, 'class':'2-E' ,'grade':'F-'},
                {'name':'s6', 'age':17, 'class':'2-F' ,'grade':'A+'},
            ]
        })
        archtemplate = TreeTemplate(processor, context, evaluator)
        rptemplate = ExcelRelpaceTemplate(processor, context, embeddedEvaluator)
        archtemplate.process()
        rptemplate.process()
        processor.save('out6.xlsx')

image

image

zongqiqi commented 6 years ago

OK,我试试,多谢,一直在找这个