【问题标题】:How can I create a table in a DOCX file with Python?如何使用 Python 在 DOCX 文件中创建表?
【发布时间】:2019-10-11 10:31:51
【问题描述】:

我正在尝试将单选按钮中的选定值发送到 .docx 文件中

导入我需要的,重点是docx

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

这些是我需要放入表格左侧的 word 文档中的选项,它们充当调查中的问题。

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

在这里,我放置了名为Yes, No & N/A 的单选按钮,它们是左侧选项的答案(上面的info 列表)以及Label 代表选项或换句话说问题..

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)

这是我的功能,创建文档并保存是最简单的部分。我的问题是我混淆了创建一个将有的表;顶部左侧的选项(来自 info)是标题(参见 RadioButtons yes, no, & N/a)。并选择数据,例如,如果 option 1 我选择了否,则将数据保存到 .docx 文件中,并选择一个(请参阅所需输出页面底部的示例)。

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

这是它打开的内容:

这是所需的输出:

编号1 表示从 tkinter 应用程序中选择的项目。但会弄清楚如何将其更改为复选框。

我对自己所处的位置有点困惑,我是使用 docx 的新手。。一直在尝试阅读 documentation.. 这就是我自己挖坑的地方。

【问题讨论】:

  • 我不明白问题出在哪里。你说的是“我有这个程序做 A,我正在尝试做 B。B 不起作用。我该怎么做 B?”我能提供的唯一答案是为您编写程序。这不是一个有意义的答案,对任何人都没有帮助。您能否提供MRE 和一份明确 说明您的问题以及您尝试解决的问题?
  • 因此,您的问题与将信息写入文件或 tkinter 无关。您是否只需要知道如何在 Python 的 .docx 文件中创建一个表格,其中您的输入是一个列表?所以你没有提供一个最小的,可重现的例子。
  • tkinter 只是一个充当调查的应用程序,.docx 文件充当从 tkinter 输入的证据。但是是的,如何根据我的循环创建表
  • 好的,所以您的问题不是“Tkinter 如何使用循环将数据保存到 DOCX 文件?”,而是“如何使用 Python 在 DOCX 文件中创建表?”
  • @EthanField 让我回到你身边,我会改进我的问题

标签: python tkinter ms-word docx


【解决方案1】:

在您当前的代码中,varsIntVars 的列表。您希望单独获取每个值而不是 vars.get()。此外,在写入 docx 文件时,您需要 info 和单选按钮的值,您可以使用索引来跟踪它们。

只需对您的代码进行最少的更改,您就可以使用这样的东西。

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

或者您可以使用字典将单选按钮映射到单元格。

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

【讨论】:

  • 您好,很抱歉回复晚了。太棒了!即使我还没有编辑我的问题..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
相关资源
最近更新 更多