python - 如何根据从 tkinter 选择的值保存 docx 文件?

标签 python tkinter docx

这是我的代码:

from tkinter import *
from docx import Document

root = Tk()



info = ["Option 1", "Option 2", "Option 3"]


vars = []
for idx,i in enumerate(info):
    var = IntVar(value=0)
    vars.append(var)
    lblOption = Label(root,text=i)
    btnYes = Radiobutton(root, text="Yes", variable=var, value=2)
    btnNo = Radiobutton(root, text="No", variable=var, value=1)
    btnNa = Radiobutton(root, text="N/A", variable=var,value=0)
    lblOption.grid(column=4,row=idx, sticky = W)
    btnYes.grid(column=1,row=idx)
    btnNo.grid(column=2,row=idx)
    btnNa.grid(column=3,row=idx)


def save():
    document = Document()

    #add table
    table = document.add_table(1, 4)
    #style table
    table.style = 'Table Grid'

    #populate header row
    heading_cells = table.rows[0].cells
    heading_cells[0].text = "Options"
    heading_cells[1].text = "Yes"
    heading_cells[2].text = "No"
    heading_cells[3].text = "N/a"

    for idx, item in enumerate(vars):
        cells = table.add_row().cells
        cells[0].text = info[idx]  # gets the option name
        val = item.get()  #radiobutton value
        if val == 2:  # checks if yes
            cells[1].text = "*"
        elif val == 1:   # checks if no
            cells[2].text = "*"
        elif val == 0:   # checks if N/A
            cells[3].text = "*"

        for x in cells[2].text:
            if "*" in x:
                print("Failed.docx")
            elif "*" not in x:
                print("Test.docx")

savebtn = Button(root, text = "Save", command = save).grid()

root.mainloop()

这是我之前的问题:Link

我使用了其中一个答案将其与我的问题结合起来。

我想要实现的目标:

如果通过单选按钮为任何选项选择了no,则将文档保存为failed.docx(如果已选择每个选项而没有任何) >no's 然后将文件另存为 Test.docx

我的问题是:

为什么我的最后一个 for 循环和 if 语句不起作用。

当我选择“否”选项时,它会返回 Failed.docx。但是如果没有选择任何一个no',那么它什么也不做?根本不运行 elif 语句。

最佳答案

在您的代码中,您通过在每次迭代中创建单元格来逐行写入docx。 cells 是包含单个选项的三个单选按钮值的行。假设如果您为选项 3 选择Yes,则单元格将包含 [* '' ''] 进行迭代。

单元格[0] = *
单元格[1] = ''
单元格[2] = ''

此外,当您尝试迭代 cells[1].text 时,您正在尝试迭代空项。这就是为什么它永远不会进入 if-elif 语句,因为它永远不会进入 for 循环。
(不确定我是否能够清楚地解释这一点,但如果您使用未选定的单选按钮或调试器的值,您可以清楚地看到发生了什么)

对于解决方案,由于您想要检查单个列的所有值,因此可以使用 table.column .

for idx, item in enumerate(vars):
    cells = table.add_row().cells
    cells[0].text = info[idx]  # gets the option name
    val = item.get()  #radiobutton value
    if val == 2:  # checks if yes
        cells[1].text = "*"
        cells[2].text = "not-selected"
        cells[3].text = "not-selected"
    elif val == 1:   # checks if no
        cells[2].text = "*"
        cells[1].text = "not-selected"
        cells[3].text = "not-selected"
    elif val == 0:   # checks if N/A
        cells[3].text = "*"
        cells[1].text = "not-selected"
        cells[2].text = "not-selected"

fn = 'Test.docx'
for cell in table.columns[2].cells[1:4]: #column2 is No column, 1:4 excludes header cell
    if cell.text == '*':
        fn = 'Failed.docx'
        break

print(fn)

关于python - 如何根据从 tkinter 选择的值保存 docx 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58444796/

相关文章:

python - 如何将两个后续事件绑定(bind)到 python 中的一个回调?

python - 在 Python 中从 Word 文档 (.docx) 中提取突出显示的单词

c# - 将 TEX 文件转换为 PDF 或 DOCX?

python - 高斯混合模型 _ Scikit Learn _ 如何拟合单 D 数据?

python - 编写一个 pytest 函数来检查输出到 python 中的文件?

python - 将 Keras 与 tensorflow 后端一起使用时如何控制内存?

python - 使用 PIL 的 ImageDraw 模块

Python:根据整数范围的值在 Pandas 数据框中创建组列

python - 带有 Tkinter 的 Python 中的小部件表

javascript - 如何从 Node.js 下载 AngularJs Controller 中的 .docx 文件