How can I create a table in a DOCX file with Python?

Viewed 992

I am trying to send selected values from radiobuttons into a .docx file

importing what I need, focus is on docx

import tkinter as tk
from docx import Document
main = tk.Tk()

these are my options that I need to place into a word document on the left of the table, they act as questions in a survey.

info = ["option 1", "option 2", "option 3", "option 4"
         ]

Here I am placing radiobuttons called Yes, No & N/A which are answers to the options on the left(list of info above) and also Label to represent options or in other words questions..

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

Here is my function, creating a document and saving is the easy part. My issue is that I am muddled up creating a table that will have; Options on the left (from info) at the top are the headers (see RadioButtons yes, no, & N/a). And selected data, as an example, if for option 1 I have selected No, then save the data into a .docx file with the one been selected (See example bottom of page at Desired output).

def send():
    document = Document()
    section = document.sections[0]
    #add table
    table = document.add_table(1, 4)
    #style table
    table.style = 'Table Grid'

    #table data retrived from Radiobuttons
    items = vars.get()

    #populate header row
    heading_cells = table.rows[0].cells
    heading_cells[0].text = "Options"
    heading_cells[1].text = btnYes.cget("text")
    heading_cells[2].text = btnNo.cget("text")
    heading_cells[3].text = btnNa.cget("text")

    for item in items:
        cells = table.add_row().cells
        cells[0].text = #Options
        cells[1].text = #Yes values
        cells[2].text = #No values
        cells[3].text = #N/A values
    #save doc
    document.save("test.docx")

#button to send data to docx file
btn = tk.Button(main, text="Send to File", command= send)
btn.grid()

main.mainloop()

this is what it opens up:

enter image description here

Here is the desired output:

enter image description here

Number 1 represents selected items from the tkinter application. But will figure out how to change it to a tick box.

I am kinda confused where I am at, I am new using docx.. been trying to read the documentation.. and this is where I digged my self a hole into.

1 Answers

In your current code, vars is a list of IntVars. You want to get each value individually instead of vars.get(). Also when writing to docx file, you need both info and values of radiobuttons, to track them both you can use an index.

With minimal changes to your code, you can use something like this.

def send():
    ...
    ... 
    heading_cells[3].text = btnNa.cget("text")

    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 = "1"
        elif val == 1:   # checks if no
            cells[2].text = "1"
        elif val == 0:   # checks if N/A
            cells[3].text = "1"

    #save doc
    document.save("test.docx")

or you can use a dictionary to map radiobuttons to cells.

valuesCells = {0: 3, 1: 2, 2: 1}  # value of radiobutton: cell to write
                                  # hard to read what's going on though
for idx, item in enumerate(vars):

    cells = table.add_row().cells
    cells[0].text = info[idx]  # gets the option name
    val = item.get()
    cells[valuesCells[val]].text = "1"

#save doc
document.save("test.docx")
Related