使用openpyxl格式化日期

时间:2015-06-20 17:14:25

标签: python date openpyxl

您好我正在尝试创建一个python程序,它使用openpyxl验证excel表(1)start_date和(2)end_date中的两个单元格,以检查current_date是否介于两者之间。有没有人对如何做到这一点有任何指示?

我无法将日期输入到我可以操作的变量中。当我把这一行:

B2包含日期

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

日期等于'无'

所以任何帮助都会非常感谢你。

  ###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 个答案:

答案 0 :(得分:0)

我会说你只需要摆脱你的print

date = sheet_ranges["B2"].value

print会返回None,这就是您获取该值的原因,而不是单元格中的值。