Martin Noah
Martin Noah

Reputation: 167

Comparing Excel dates to current date in Python

Python newbie here! :)

Basically, I am trying to scan an excel file's column A (which contains all dates) and if the date in the cell is 7 days in the future...do something. Since I am learning, I am just looking at one cell before I progress and start looping through the data.

Here is my current code which isn't working.

import openpyxl, smtplib, datetime, xlrd
from openpyxl import load_workbook
from datetime import datetime


wb = load_workbook(filename = 'FRANKLIN.xlsx')
sheet = wb.get_sheet_by_name('Master')
msg = 'Subject: %s\n%s' % ("Shift Reminder", "Dear  a rem ")
cell = sheet['j7'].value

if xlrd.xldate_as_tuple(cell.datemode) == datetime.today.date() + 7:
print('ok!')

Here is the error code I am getting: 'datetime.datetime' object has no attribute 'datemode'

I've tried searching high and low, but can't quite find the solution.

Upvotes: 0

Views: 2618

Answers (1)

masnun
masnun

Reputation: 11906

Your cell variable seems to be datetime.datetime object. So you can compare it like this:

from datetime import timedelta

if cell.date() == (datetime.now().date() + timedelta(days=7)):
    print("ok")

Upvotes: 2

Related Questions