Skip to content

Instantly share code, notes, and snippets.

@timothycarambat
Created February 5, 2019 06:19
Show Gist options
  • Save timothycarambat/028bd4ccb4fcd1c9216aca20d4e21f64 to your computer and use it in GitHub Desktop.
Save timothycarambat/028bd4ccb4fcd1c9216aca20d4e21f64 to your computer and use it in GitHub Desktop.
Python GUI Directory Monitor that automatically gets the word count across all DOCX files in a directory! /r/programming request for /u/punter45 LINK:https://www.reddit.com/r/programmingrequests/comments/amuwfe/request_microsoft_word_word_count_tracker/
from docx import Document
from Tkinter import *
import os
def get_documents(files):
file_list = []
for document in files:
if ".docx" in document and "$" not in document:
file_list.append(document)
return file_list
def report_values(file_list, path):
word_count = 0
for doc_file in file_list:
document = Document("%s\\%s" % (path, doc_file))
doc_text = '\n\n'.join([
paragraph.text.encode('utf-8') for paragraph in document.paragraphs
])
doc_word_count = len(doc_text.split(' '))
word_count += doc_word_count
return word_count
def get_wc_objective(value):
if value == '':
return 0
else:
return value
def calc_progress(count, objective):
if objective == 0:
return "--"
else:
calc = float(count/objective)*100
return "%6.2f" % calc
class Interface:
def __init__(self, master):
self.master = master
master.title("Python DOCX Word Counter")
master.geometry("500x500")
self.word_count = 0
self.wc_objective = 0
#this variable determines how quickly the script reads the value
self.refresh_timer = 500
L1 = Label(master, text = "Document Directory")
L1.grid(row=0)
L2 = Label(master, text="Word Count Objective (optional)")
L2.grid(row=1)
self.E1 = Entry(master, width=50)
self.E1.insert(END, "Document\\Directory\\Goes\\Here\\")
self.E1.grid(row=0, column=1)
self.E2 = Entry(master, width=50)
self.E2.insert(END, 0)
self.E2.grid(row=1, column=1)
self.B1 = Button(master, text="Start Recording", command=self.start)
self.B1.grid(row=3)
self.B2 = Button(master, text="Close Program", command=master.quit)
self.B2.grid(row=3)
self.prog_text = StringVar()
self.prog_text.set("Progress is %d/%s" % (self.word_count, self.wc_objective))
self.total_text = StringVar()
self.total_text.set("Total Word Count is %d" % (self.word_count))
self.L3 = Label(master, textvariable = self.prog_text)
self.L3.grid(row=5)
self.L4 = Label(master, textvariable = self.total_text)
self.L4.grid(row=6)
L1.pack()
self.E1.pack()
L2.pack()
self.E2.pack()
self.B1.pack()
self.B2.pack()
self.L3.pack()
self.L4.pack()
def start(self):
path = self.E1.get()
self.wc_objective = get_wc_objective(self.E2.get())
self.B1.config(state="disabled")
files = os.listdir(path)
file_list = get_documents(files)
self.word_count = report_values(file_list, path)
self.prog_text.set("Progress is %d/%s (%s%%)" % (self.word_count, self.wc_objective, calc_progress(float(self.word_count), float(self.wc_objective)) ))
self.total_text.set("Total Word Count is %d" % (self.word_count))
root.after(self.refresh_timer, self.start)
root = Tk()
my_gui = Interface(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment