You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PySimpleGUI TabGroup字典致docxtpl渲染错误及解决

使用PySimpleGUI TabGroup时docxtpl渲染报错的解决办法

我用PySimpleGUI结合docxtpl开发GUI工具,实现用户输入渲染到Word文档的功能。但用TabGroup组织界面时,调用doc.render(values)出现如下错误:

Traceback (most recent call last):
File "C:\Users\SESA484891\Desktop\doc render practice\DP-docrenderpractice.py", line 27, in 
doc.render(values)
TypeError: keywords must be strings

移除TabGroup后渲染正常,同时想了解:每个标签页是否有独立字典?该如何访问?

相关代码

from pathlib import Path
import PySimpleGUI as sg
from docxtpl import DocxTemplate

document_path = Path(__file__).parent / "DP-docrenderpractice.docx"
doc = DocxTemplate(document_path)

# TAB
tab1 = [
    [sg.Text("Proposal Name:"),sg.Input(key="PROPOSAL_NAME",size=(30,0))]
]
# LAYOUT 
layout = [[sg.TabGroup([
    [sg.Tab('Tab', tab1)]])],
    [sg.Button("Create Template"), sg.Exit()],
]
##########################

window = sg.Window("Doc Render Practice", layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == "Exit":
        break
    if event == "Create Template":
        # Render the template, save new word document & inform user
        doc.render(values)
        output_path = Path(__file__).parent / f"DP-{values['PROPOSAL_NAME']}.docx"
        doc.save(output_path)
        sg.popup("File saved", f"File has been saved here: {output_path}")

window.close()

完整报错回溯

=============================================================================RESTART: C:\Users\SESA484891\Desktop\doc render practice\DP-docrenderpractice.py==================================================
Traceback (most recent call last):
  File "C:\Users\ SESA484891\Desktop\doc render practice\DP-docrenderpractice.py", line 27, in <module> 
    doc.render (values)
  File "C:\Users\ SESA484891\AppData\Local\Packages\Python Software Foundation. Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\docxtpl\template.py", line 368, in render 
    xml src= self.build_xml (context, jinja_env)
  File "C:\Users\ SESA484891\AppData\Local\Packages\Python Software Foundation. Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\docxtpl\template.py", line 315, in build_xml 
    xml = self.render_xml_part (xml, self.docx._part, context, jinja_env)
  File "C:\Users\ SESA484891\AppData\Local\Packages\Python Software Foundation. Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\docxtpl\template.py", line 239, in render_xml_part 
    dst_xml = template.render (context) 
  File "C:\Users\ SESA484891\AppData\Local\Packages\PythonSoftware Foundation. Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\jinja2\environment.py", line 1296, in render 
    ctx = self.new_context (dict (*args, **kwargs))
  File "C:\Users\ SESA484891\AppData\Local\Packages\PythonSoftware Foundation. Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\jinja2\environment.py", line 1388, in new_context 
    return new context 
  File "C:\Users\SESA484891\AppData\Local\Packages\PythonSoftware Foundation. Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\jinja2\runtime.py", line 106, in new_context 
     parent = dict (globals or (), **vars)
TypeError: keywords must be strings

解决方法

已解决!
打印values字典发现,PySimpleGUI会给TabGroup自动添加键0(对应当前选中标签页的索引),而docxtpl要求上下文字典的键必须是字符串,这个整数键引发了冲突。通过删除该键即可解决问题,修改后的渲染逻辑代码如下:

if event == "Create Template":
    # 移除TabGroup自动添加的整数键
    values.pop(0, None)
    # 渲染模板、保存文档并提示用户
    doc.render(values)
    output_path = Path(__file__).parent / f"DP-{values['PROPOSAL_NAME']}.docx"
    doc.save(output_path)
    sg.popup("File saved", f"File has been saved here: {output_path}")

关于标签页独立字典的问题:PySimpleGUI的TabGroup不会为每个标签页生成独立字典,所有控件的键都会统一存入values字典。若需区分不同标签页的输入,可给控件键添加标签页前缀(如TAB1_PROPOSAL_NAME),以此在values中区分来源。

内容的提问来源于stack exchange,提问作者Evan Lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 00:06:26