timesheetpy/getHours.py
2021-03-16 22:07:41 -04:00

90 lines
2.7 KiB
Python

import openpyxl;
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class AppWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hours Reporting Tool")
self.set_border_width(10)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
chooseFileBtn = Gtk.Button.new_with_label("Hours File")
chooseFileBtn.connect("clicked", self.onChooseFileClicked)
hbox.pack_start(chooseFileBtn, False, False, 0)
def onChooseFileClicked(self, button):
dialog = Gtk.FileChooserDialog(title="Choose a file", parent=self, action=Gtk.FileChooserAction.OPEN)
dialog.add_buttons(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.OK,
)
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
filePath = dialog.get_filename()
print("Loading: " + filePath)
dialog.destroy()
self.runExcelFiltering(filePath)
def add_filters(self, dialog):
filter_excel = Gtk.FileFilter()
filter_excel.set_name("Excel Files")
filter_excel.add_pattern("*.xlsx")
filter_excel.add_pattern("*.xls")
dialog.add_filter(filter_excel)
def runExcelFiltering(self, file):
wb = openpyxl.load_workbook(file)
sheet = wb.active
namecolumn = sheet['E']
datecolumn = sheet['F']
hourscolumn = sheet['G']
report=[]
for x in range(1,sheet.max_row):
currentName = namecolumn[x].value
currentDate = datecolumn[x].value
currentHours = hourscolumn[x].value
if not any (entry['name'] == currentName for entry in report):
report.append({
"name" : currentName,
"entries" : [{
"date":currentDate,
"hours":currentHours
}]
})
else:
for entry in report:
if entry['name'] == currentName:
entry['entries'].append({
"date":currentDate,
"hours":currentHours
})
for entry in report:
print(entry['name'])
hoursTotal = 0
for time in entry['entries']:
hoursTotal += int(time['hours'])
print(str(time['date']) + ", " + time['hours'])
print("Total Worked Hours: " + str(hoursTotal))
print("------------------------------")
win = AppWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()