如何在Python中基于if语句保存一个文档? [英] How to to save one document based on an if statement in Python?

查看:70
本文介绍了如何在Python中基于if语句保存一个文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于if语句保存文档.

I am trying to save a document based on a if statement.

我在这里创建单选按钮:

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个单选按钮中,是,否,不适用.只能选择一个.
  • 接下来,当按下按钮 save ..时,它将在 docx 中创建一个表,选项位于 row 0 中,并与所选内容一起追加是,否&的值不适用.
  • 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 in docx, Options is in row 0 appending down along with selected values of Yes, no & N/a.

例如:

Options       Yes      No     N/a
Option 1       *
Option 2               *
Option 3       *

我的问题:

我只需按 save ,它将文件保存为 test.docx .

I can simply press save and it saves the file as test.docx.

现在,我正在尝试弄清楚如何将文件另存为 Failed.docx

Now, I am trying to figure out how to save the file as Failed.docx

仅当所有选项中的一个或多个选项都选择了 no 值时,才会创建 Failed.docx .

The Failed.docx will only be created if one or more out of all the options has a no value selected.

在下面的示例中,该文件将另存为 Test.docx ,因为没有一个选项选择了 no 值:

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       *

下面的示例将其保存为 Failed.docx ,因为已为左侧的选项之一选择了 no 选项.

An example below, this would be saved as Failed.docx, because no option has been selected for one of the options on the left.

例如:

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] 中的 * (这是链接到 No 值的第2行).

This detects * within the cell[2] (This is row 2 linked to No value).

如果选择了'no'值,它会输出true,但也会输出false

And if a 'no' value has been selected it prints out true but also prints out false

例如:

Options       Yes      No     N/a
Option 1       *
Option 2               *
Option 3       *

for循环的输出:

Output of the for loop :

False
True
False

但是,如果它检测到 False True ,则两个文件都将被保存.我完全困惑从哪里去.

But if it detects False and True both files will be saved. I am totally confused where to go from here..

推荐答案

问题:只有在所有选项中的一个或多个中,'Failed.docx'才会被创建code>已选择 no 值.

Question: The 'Failed.docx' will only be created if one or more out of all the options has a no value selected.

可以改写为:

     if any option has NO

您已经根据条件 value == NO
建立了 Boolean list [False,True,False]

You have build a list of Boolean from the condition value == NO,
like [False, True, False]

任何(可迭代)

如果iterable的任何元素为true,则返回True.如果iterable为空,则返回False.

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')

输出:

[2, 0, 2]
test.docx

[2, 1, 2]
Failed.docx

这篇关于如何在Python中基于if语句保存一个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆