Search code examples
pythontkinterdocx

How to save docx file based on values chosen from tkinter?


Here is my code:

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

This is my previous question: Link

I have used one of the answers to combine it with my question.

What I am trying to achieve:

If no has been selected for any of the options via the radio buttons, then save the document as failed.docx if every option has been selected without any no's then save the file as Test.docx.

My problem is:

Why is my last for loop and if statement is not working.

When I select a no option it returns me Failed.docx. But if none of the no' is selected, it does nothing? does not run the elif statement at all.


Solution

  • In your code, you are writing to docx row by row by creating cells on each iteration. cells is a row that contains three radiobutton values for a single option. Let's say if you choose Yes for option3, cells would contain [* '' ''] for its iteration.

    cells[0] = *
    cells[1] = ''
    cells[2] = ''

    Also when you try to iterate over cells[1].text, you are trying to iterate on an empty item. That's why it never gets into if-elif statements because it never gets into for loop.
    (not sure if I managed to explain this clearly but if you use a value for non-selected radiobuttons or debugger, you can see quite clearly what's going on)

    For a solution, since you want to check all values for a single column, you can use 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)