Python 3.x 使用python tkinter对.docx执行一些操作

Python 3.x 使用python tkinter对.docx执行一些操作,python-3.x,tkinter,filepath,docx,glob,Python 3.x,Tkinter,Filepath,Docx,Glob,我创建了一个脚本,用于从用户处获取路径名,并执行一些.docx操作。当我运行脚本时,它给出了该文件夹中.docx的总数,但它不适用于表计数。我不知道我对tkinter使用的代码是否正确。我已经尝试了pathlib和os.path,但它不起作用 这是我的密码: import os import glob import os.path from docx import Document from tkinter import * def print_

我创建了一个脚本,用于从用户处获取路径名,并执行一些.docx操作。当我运行脚本时,它给出了该文件夹中.docx的总数,但它不适用于表计数。我不知道我对tkinter使用的代码是否正确。我已经尝试了pathlibos.path,但它不起作用

这是我的密码:

    import os
    import glob
    import os.path
    from docx import Document

    from tkinter import *
    def print_input():

#To print how many files with .docx extension in the folder 

        mypath = text_entry.get()
        files=0
        for name in os.listdir(mypath):
            if name.endswith('.docx'):
              files=files+1
        print("Total No of Files:",files)

        #To read the .docx and print how many tables in that 

        table=0
        for name in glob.glob('/*.docx'):
          doc=Document(name)
          for t in doc.tables:
            for ro in t.rows:
             if ro.cells[0].text=="ID" :
                table=table+1
          print("Total Number of Tables: ", table)

    root = Tk()
    Label(root, text="Enter Path").grid(row=0)

    text_entry = Entry(root)
    text_entry.grid(row=1, column=0)
    text_entry.config(background="yellow", foreground="blue")
    Button(root, text='Submit', command=print_input).grid(row=3, column=0, sticky=W, pady=4)
    mainloop()
当我尝试在glob中指定路径名时,它正在工作,但当我尝试从文本框传递值时,它没有执行,而是给出正确的详细信息,它显示随机数。希望你能理解我的问题

我不知道“不起作用”是什么意思,但你从“我的路径”中得到了名字

但是表来自“/*.docx”

for name in glob.glob('/*.docx'):
只需一次操作即可完成(并查看filedialog.askdirectory)


对于glob.glob(mypath+'/*.docx')中的名称:
应该可以使用,您可以给出它显示的随机数的示例吗?@PRMoureu您可以保存我的一天吗。非常感谢您当我尝试将其作为一个操作时,我发现错误:“在“%s”处找不到包%pkg_文件docx.opc.exceptions.PackageNotFoundError:在“third.docx”处找不到包请编辑您的完整代码好吗@Curly JoeAgain,我不知道“当我尝试将其作为一个操作时,我发现了错误:”是什么意思或它指的是什么行。您可能必须提供文件的完整路径,以便将os.path.join(mypath,name)添加到Document()。也要打印它,以确保它是去你想去的地方。
for name in glob.glob('/*.docx'):
    mypath = text_entry.get()
    files=0
    for name in os.listdir(mypath):
        if name.endswith('.docx'):
          files=files+1
    #print("Total No of Files:",files)

    #To read the .docx and print how many tables in that 

          table=0
    ##for name in glob.glob('/*.docx'):
          doc=Document(name)
          for t in doc.tables:
              for ro in t.rows:
                  if ro.cells[0].text=="ID" :
                      table=table+1