Formatting date with openpyxl

4.1k Views Asked by At

Hello I am trying to create a python program which verifies two cells in an excel sheet (1)start_date and (2)end_date within an excel sheet using openpyxl to check if current_date is in between. Does anyone have any pointers on how I can do this?

I am having trouble getting the date into a variable I can manipulate. When I put this line:

B2 contains a date

date = print(sheet_ranges["B2"].value)

date is then equal to 'None'

So any help would be great thank you.

  ###Adam Giancola
###June 16th 2015
###This program will open and excel file and play with some data in the cells
###This documentation is helpful:
#   http://openpyxl.readthedocs.org/en/latest/usage.html#read-an-existing-workbook



#VERSION 1: This program can read an inputted number and track the nuber of times it is used

#VERSION 2: This program will check if the coupon is expired

import sys
import openpyxl
from openpyxl import Workbook
from datetime import datetime
from openpyxl import load_workbook
import re

try:
    while(1):
        
        

        
        match_flag = 0
        #Assign varialble to both Workbook & Sheet
        wb = openpyxl.load_workbook(filename = 'coupon_book.xlsx')
        sheet_ranges = wb.get_sheet_by_name('Sheet1')

        date = print(sheet_ranges["B2"].value)
        print(date)
        
        #Print data from an array
        #print(sheet_ranges['A1'].value)


        input_value = eval(input("Hello please enter a coupon code: "))
        #Reads inputs as a number rather than a character



        for row in sheet_ranges.iter_rows():
           #data = row[0].value   #no need for this line
            
                if row[0].value == input_value:
                        
                        match_flag = 1
                        temp = row[3].value
                        row[3].value = temp - 1
                        if match_flag == 1:
                            print("We have a match!")
                 
                           

                
        if match_flag == 0:
            print("Sorry your coupon is not valid")
                    

        wb.save('coupon_book.xlsx')




    
1

There are 1 best solutions below

4
On

I would say that you simply need to get rid of your print:

date = sheet_ranges["B2"].value

print returns None, so that's why you're getting that value, rather than the value from the cell.