title: How can I create a table in a DOCX file with Python?
body: <p>I am trying to send selected values from radiobuttons into a <code>.docx</code> file</p>
  
  <p>importing what I need, focus is on docx</p>
  
  <pre><code>import tkinter as tk
  from docx import Document
  main = tk.Tk()
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>info = ["option 1", "option 2", "option 3", "option 4"
           ]
  </code></pre>
  
  <p>Here I am placing radiobuttons called <code>Yes, No &amp; N/A</code> which are answers to the options on the left(list of <code>info</code> above) and also <code>Label</code> to represent options or in other words questions..</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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 <code>info</code>) at the top are the headers (see RadioButtons <code>yes, no, &amp; N/a</code>). And selected data, as an example, if for <code>option 1</code> I have selected No, then save the data into a <code>.docx</code> file with the one been selected <strong>(See example bottom of page at Desired output).</strong></p>
  
  <pre><code>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()
  </code></pre>
  
  <p>this is what it opens up:</p>
  
  <p><a href="https://i.stack.imgur.com/VyifL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VyifL.jpg" alt="enter image description here"></a></p>
  
  <p>Here is the desired output:</p>
  
  <p><a href="https://i.stack.imgur.com/1CdLB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1CdLB.jpg" alt="enter image description here"></a></p>
  
  <p>Number <code>1</code> represents selected items from the tkinter application. But will figure out how to change it to a tick box.</p>
  
  <p>I am kinda confused where I am at, I am new using docx.. been trying to read the <a href="https://buildmedia.readthedocs.org/media/pdf/python-docx/latest/python-docx.pdf" rel="nofollow noreferrer">documentation</a>.. and this is where I digged my self a hole into.</p>