我已经创建了一个脚本,用于从用户获取路径名并执行一些.docx操作。当我运行脚本时,它给出了该文件夹中.docx的总数,但它不适用于表计数。我不知道我在tkinter中使用了正确的代码。我已经尝试过路径库和os.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中指定路径名时,它正在工作,但是当我试图从textbox传递值时,它不是在执行,而是给出正确的细节,它显示随机数字。希望你能理解我的问题
发布于 2017-10-31 20:45:17
不知道“不工作”是什么意思,但是你从"mypath“中得到了名字
for name in os.listdir(mypath):但是表格来自/*..docx
for name in glob.glob('/*.docx'):在一次操作中执行(并查看filedialog.askdirectory)
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+1https://stackoverflow.com/questions/47043834
复制相似问题