0

I have a program I already wrote to send text messages to a client as a demo, no actual text were sent. Now I need the program to send the texts when it is a specific time of day every day. I have made an excel file with all my clients names, when, they need to be texted and all the other essential information I need to run each of my programs. I'm trying to use a conditional statement that basically says if the current time matches the date in column "Med Time 1", then run the first definition I have which will send the text to start the conversation. I'm having syntax error every time I try anything. This is what I have so far without all the coding for the program I will run since I can't get past the conditional statement. I tried adding a column in the Excel file with the "Current date". That has not worked either. I've been stumped for two weeks.

import pandas as pd
import datetime

#Assign spreadsheet filename
file = 'Uganda_Data.xlsx'

#Load spreadsheet
xl = pd.ExcelFile(file)

#Parse the first sheet
df = xl.parse('Sheet1')

# Medication
if datetime.datetime.now() == df[['Current date','Med Time 1']]
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • Are you asking *how to send text messages* **or** *how run code at a scheduled time* **or** *how to retrieve times from Excel with Python*?? – ashleedawg Mar 21 '18 at 20:07
  • run a task at a specific time? I have several clients on a database in excel. Each person has a specific time that they are to be contacted to start my program, which is one of the columns in the excel file. I need to be able to translate from the excel file column to something I can compare the current either datetime.datetime.now() or datetime,datetime.now().time() – Elizabeth Bird Rock Mar 21 '18 at 20:13
  • or some other way to let my programs definition to know to run. currently it's in demo mode, so I have a button that I push in tkinter to start the program. I need it to be a working system now and not a demo. – Elizabeth Bird Rock Mar 21 '18 at 20:14
  • How to schedule jobs in Python: [https://stackoverflow.com/a/16786600/8112776](https://stackoverflow.com/a/16786600/) – ashleedawg Mar 21 '18 at 20:17
  • Hey @ElizabethBirdRock, where are you running this code? Will it be run locally on your machine, or is it going to be on a server somewhere? – Peter Dolan Mar 21 '18 at 21:20
  • excuse me, I was told to make a demo first, until we got our grant money. Now I have to make a working product. I only asked for help because I've been stuck for two weeks with error after error. I didn't expect someone else to do my project for me. I just needed help figuring out what I'm doing wrong. – Elizabeth Bird Rock Mar 22 '18 at 17:48
  • I'm running it on my computer, but eventually I'm hoping it will be on a server. – Elizabeth Bird Rock Mar 22 '18 at 17:49

1 Answers1

0

The class datetime.datetime.now() returns a object containing all the information from the date (year,month,day,hour,minute,second,microsecond).

Suppose that "now" is 3/21/2018 6:10 PM and you call (note the precision of seconds and microseconds):

>>> datetime.datetime.now()
datetime.datetime(2018, 3, 21, 18, 10, 05, 335514)

And you wrote and saved an excel file like this at the same time (using the function =now() for Currente Date):

User    Current Date    Med Time 1
user 1  3/21/18 6:00 AM 3/21/18 17:29 AM
user 2  3/21/18 6:00 AM 4/21/18 4:00 AM
user 3  3/21/18 6:00 AM 4/5/18 5:00 PM

Now you load the file using pandas.read_excel() (instead pandas.ExcelFile).

>>> import pandas as pd
>>> import datetime

>>> df = pd.read_excel('Uganda_Data.xlsx')
>>> df
     User        Current Date          Med Time 1
0  user 1 2018-03-21 06:00:00 2018-03-21 18:10:05
1  user 2 2018-03-21 06:00:00 2018-04-21 04:00:00
2  user 3 2018-03-21 06:00:00 2018-04-05 17:00:00

Note that Med Time 1 for user 1 is another type of object (is Timestamp but not Datetime.Datetime)

>>> df['Med Time 1'][0]
Timestamp('2018-03-21 18:10:05')

Note that Current Date at df is the date\time at the time you save the excel file, and will never be equals to the time you call datetime.datetime.now().

So, you should convert dates, e.g. using

>>> pd.Timestamp(datetime.datetime.now())
Timestamp('2018-03-21 18:40:42.639488')

Code Suggestion

import pandas as pd

#Assign spreadsheet filename
file = 'Uganda_Data.xlsx'

#Load spreadsheet
xl = pd.ExcelFile(file)

#Parse the first sheet
df = xl.parse('Sheet1')

# Filter users that are on time to take the medicine
df_now = df[df['Med Time 1'] == pd.Timestamp.now()]

# Do something...
for line in df_now:
    print("Call the user {}".format(df['Users']))
    ...

Note: be careful about what is in Med Time 1. My suggestion is that you keep time as hour:minute without seconds and microseconds. You should code (when and if necessary):

df['Med Time 1'].map(lambda t: t.strftime('%Y-%m-%d %H:%M'))
pd.Timestamp.now().strftime('%Y-%m-%d %H:%M')