我正在尝试根据 if 语句保存文档。
我在这里创建单选按钮:
info = ["Option 1", "Option 2", "Option 3"]
vars = []
for idx,i in enumerate(info):
var = IntVar(value=0)
vars.append(var)
lblOption = Label(main,text=i)
btnYes = Radiobutton(main, text="Yes", variable=var, value=2)
btnNo = Radiobutton(main, text="No", variable=var, value=1)
btnNa = Radiobutton(main, 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)
我在这里创建一个文档
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 = "*"
#save doc
document.save("test.docx")
幕后工作:
- 在 3 个单选按钮 Yes、No、N/a.. 中只能选择一个。
- 接下来,当按下按钮时
save
..它创建了一个表docx
, 选项在row 0
与“是”、“否”和“不适用”的选定值一起向下追加。
举个例子:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
我的问题:
我可以简单地按save
并将文件另存为test.docx
.
现在,我想弄清楚如何将文件另存为Failed.docx
The Failed.docx
仅当所有选项中的一个或多个选项具有no
选定的值。
如下例所示,这将被保存为Test.docx
,因为没有一个选项具有no
选择的值:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
下面的例子,这将被保存为Failed.docx
, 因为no
已为左侧选项之一选择了选项。
举个例子:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
这是我到目前为止所尝试过的:
for x in cells[2].text:
if "*" in x:
print("True")
else:
print("False")
这检测到*
内cell[2]
(这是第 2 行链接到No
value).
如果选择了“否”值,它会打印出 true,但也会打印出 false
举个例子:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
输出的for loop
:
False
True
False
但如果它检测到False
and True
这两个文件都将被保存。我完全困惑从这里该去哪里..