Advertisement
TorroesPrime

Untitled

Sep 29th, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. """ACE appointment class module"""
  2. from datetime import datetime
  3. from openpyxl import Workbook
  4. import pandas as pd
  5. import openpyxl
  6. import os
  7. from rich import inspect
  8. class appointment():
  9.     def __init__(self,day,month,year,subject,tutor_name,screen_name):
  10.         self.day = day
  11.         self.month =month
  12.         self.year = year
  13.         self.subject = self.santize_subject(subject)
  14.         self.tutor_name=tutor_name
  15.         self.tutor_screen_name = screen_name
  16.         self.day_of_the_week = self.get_day_of_the_week()
  17.     def get_day_of_the_week(self):
  18.         """ returns the day of the week from the date of the appointment"""
  19.         date = pd.Timestamp(str(self.year)+"-"+str(self.month)+"-"+str(self.day))
  20.         return date.day_name()
  21.     def santize_subject(self,subject):
  22.         if type(subject) is None:
  23.             return "[SUBJECT NOT RECORDED]"
  24.         else:
  25.             return subject
  26.     def check_within_date_range(self,earliest_date,latest_date):
  27.         app_date = datetime(self.year,self.month,self.day)
  28.         if earliest_date < app_date < latest_date:
  29.             return True
  30.         else:
  31.             return False
  32. def check_for_file(file_name):
  33.     """checks if a supplies file exists in the current directory"""
  34.     if os.path.exists(file_name):
  35.         return True
  36.     else:
  37.         return False
  38.  
  39. def read_excel_data(file_name):
  40.     """reads an excel file to create appointments and places them into a list."""
  41.     curr_path = os.getcwd()
  42.     file_to_check = curr_path+"\\"+file_name
  43.     appointment_list = []
  44.     if check_for_file(file_to_check):
  45.         workbook = openpyxl.load_workbook(file_to_check)
  46.         worksheet = workbook["sheet 1"]
  47.         row = 2
  48.         while row < 17916:
  49.             row = str(row)
  50.             date_day = worksheet[str("A"+row)].value.day
  51.             date_month = worksheet[str("A"+row)].value.month
  52.             date_year = worksheet[str("A"+row)].value.year
  53.             subject = worksheet[str("B"+row)].value
  54.             tutor = worksheet[str("C"+row)].value
  55.             screen_name = worksheet[str("D"+row)].value
  56.             appointment_list.append(appointment(date_day,date_month,date_year,subject,tutor,screen_name))
  57.             row = int(row)+1
  58.         return appointment_list
  59.  
  60. def search_by_data_range_and_subject(appointments_list,starting_date,ending_date):
  61.     results = {}
  62.     for appoint in appointments_list:
  63.         if appoint.subject in results.keys():
  64.             results[appoint.subject] += 1
  65.         else:
  66.             results.update({appoint.subject: 1})
  67.     return results
  68.  
  69. def write_a_series_appointment_data_to_excel(list_of_data,filename = "appointment_data.xlsx",worksheet_name = "data"):
  70.     wb = Workbook()
  71.     col_to_write = 1
  72.     for item in list_of_data.keys():
  73.         write_to_excel_file(list_of_data[item],wb,item,col_to_write)
  74.         col_to_write += 2
  75.     wb.save(filename)
  76.     wb.close()
  77.  
  78. def write_to_excel_file(source_data,workbook : Workbook,section_header : str,start_col : int):
  79.     wb = workbook
  80.     col = start_col
  81.     row = 2
  82.     subject = 1
  83.     num_of_apointments = 2
  84.     ws01 = wb.active
  85.     ws01.title="raw data"
  86.     _ = ws01.cell(column=start_col,row=row-1,value = section_header)
  87.     while row<len(source_data.keys()):  
  88.         for item,val in source_data.items():
  89.             _ = ws01.cell(column=start_col,row=row,value = item)
  90.             _ = ws01.cell(column=start_col+1,row=row,value = val)
  91.             row += 1
  92.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement