使用Python生成动态Word文档时处理空格

问题描述

我有我的这个迷你个人项目,我想使用python生成动态Word文档

这是我的代码的一部分

<some code before>

       tpl = DocxTemplate(os.path.dirname(os.path.abspath(__file__))+r'\template.docx')
        context = { 'a' : input('enter value for a'),'b' : input('enter value for b'),'c' : input('enter value for c'),'d' : input('enter value for d'),'e': input('enter value for e'),'f' : input('enter value for f')
                    }
            
        tpl.render(context)
        tpl.save(os.path.dirname(os.path.abspath(__file__))+r'\generated.docx')

<some code after>

我的问题是,如果我输入的字段为空,它将被解释为我想将该特定字段更改为空。

template: 
this is a variable => {{a}} 

after running code but i did not put anything to input('enter value for a')
this is a variable => 

有没有办法对python说,如果它为空,那么不要替换它?我。

具体

template: 
this is a variable => {{a}} 

after running code but i did not put anything to input('enter value for a')
this is a variable => {{a}} 

after running code but entered something to input('enter value for a')
this is a variable => this is what we have inputted

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)