Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- #
- # assignChores.py - email a random list of chores to a list of emails from an excel spreadsheet
- #
- # Write a program that takes a list of people’s email addresses and a list of chores
- # that need to be done and randomly assigns chores to people. Email each person their
- # assigned chores.
- #
- # If you’re feeling ambitious, keep a record of each person’s previously assigned
- # chores so that you can make sure the program avoids assigning anyone the same chore
- # they did last time.
- #
- # For another possible feature, schedule the program to run once a week automatically.
- #
- # Note:
- # - smtp_info file has each item on separate line
- # - use input() for password to prevent storing in unencrypted file
- # - it may be easier to:
- # - setup a crontab to run weekly
- # - store saved_time and prev_chores in a shelve database
- #
- # This program is part of a conversation about a python book on reddit at:
- # https://www.reddit.com/r/inventwithpython/comments/8ykq1i/automate_the_boring_stuff_with_python_corrections/
- #
- # -/u/JoseALerma
- #
- import openpyxl, random, smtplib, datetime
- # Open the spreadsheet and get the lists of data.
- wb = openpyxl.load_workbook('choresList.xlsx')
- sheet = wb['Sheet1']
- names, emails, chores, prev_chores = [], [], [], []
- for row in range(2, sheet.max_row + 1): # skip title row
- name = sheet['A' + str(row)].value
- email = sheet['B' + str(row)].value
- chore = sheet['C' + str(row)].value
- prev_chore = sheet['D' + str(row)].value
- names.append(name)
- emails.append(email)
- chores.append(chore)
- prev_chores.append(prev_chore)
- # Run weekly
- saved_time = sheet['E2'].value
- interval = datetime.timedelta(days=7)
- now = datetime.datetime.now()
- if saved_time is None:
- saved_time = now - interval # First run, so it's been a week
- timedelta = saved_time + interval
- if timedelta > now:
- time_left = round((timedelta - now).total_seconds()/60, 2)
- print(f"RuntimeError: Need to wait {time_left} minutes before running again.")
- raise RuntimeError
- else:
- sheet['E2'].value = now # save to spreadsheet
- # Log in to email account.
- with open('../smtp_info') as config:
- myEmail, password, server, port = config.read().splitlines()
- smtpObj = smtplib.SMTP_SSL(server, port) # Using port 465
- smtpObj.ehlo()
- smtpObj.login(myEmail, password)
- # Randomly assign chores
- for i in range(0, len(names)):
- random_chore = random.choice(chores)
- # Check previous chore before assignment
- while random_chore == prev_chores[i] and len(chores) > 1:
- random_chore = random.choice(chores)
- # Keep track of chores assigned
- sheet['D' + str(i + 2)].value = random_chore
- chores.remove(random_chore) # remove assigned chore from pool
- # Send email.
- body = "Subject: Chore for the Week: %s.\nDear %s,\n\nThis week, you're in charge of:\n%s. " \
- "\n\nThank you in advance for your efforts!" % (random_chore, names[i], random_chore)
- print(f'Sending email to {emails[i]}...')
- sendmailStatus = smtpObj.sendmail(myEmail, emails[i], body)
- if sendmailStatus != {}:
- print(f'There was a problem sending email to {emails[i]}: {sendmailStatus}')
- smtpObj.quit()
- wb.save('choresList.xlsx')
Add Comment
Please, Sign In to add comment