问题
I am trying to save a document based on a if statement.
Here I am creating radiobuttons:
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)
Here I am creating a document
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")
Work behind the scenes:
- Out of the 3 radio-button Yes, No, N/a.. Only one can be chosen.
- Next, when pressed a button
save
.. it creates a table indocx
, Options is inrow 0
appending down along with selected values of Yes, no & N/a.
As an example:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
My Problem:
I can simply press save
and it saves the file as test.docx
.
Now, I am trying to figure out how to save the file as Failed.docx
The Failed.docx
will only be created if one or more out of all the options has a no
value selected.
As an example below, this would be saved as Test.docx
, because not a single Option has a no
value selected:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
An example below, this would be saved as Failed.docx
, because no
option has been selected for one of the options on the left.
As an example:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
Here is what I have tried so far:
for x in cells[2].text:
if "*" in x:
print("True")
else:
print("False")
This detects *
within the cell[2]
(This is row 2 linked to No
value).
And if a 'no' value has been selected it prints out true but also prints out false
As an example:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
Output of the for loop
:
False
True
False
But if it detects False
and True
both files will be saved. I am totally confused where to go from here..
回答1:
Question: The
'Failed.docx'
will only be createdif one or more
out of all theoptions
has ano
value selected.
This can be rephrased to:
if any option has NO
You have build a list
of Boolean
from the condition value == NO
,
like [False, True, False]
- Built-in - any
any(iterable)
Return True if any element of the iterable is true. If the iterable is empty, return False.
YES = 2; NO = 1; NA = 0
print(vars)
if any([v.get() == NO for v in vars]):
print('Failed.docx')
else:
print('test.docx')
Output:
[2, 0, 2]
test.docx
[2, 1, 2]
Failed.docx
回答2:
Try the following:
for x in cells[2].text:
if "*" in x:
print("Failed.docx")
elif "*" not in x:
print("Test.docx")
this checks if any "*"
is inside your no
row, if it exists save as Failed.docx
if not save as Test.docx
来源:https://stackoverflow.com/questions/58439001/how-to-to-save-one-document-based-on-an-if-statement-in-python