使用openpyx格式化日期

2024-04-16 08:45:00 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在尝试创建一个python程序来验证excel表中的两个单元格(1)开始日期和(2)结束日期,使用openpyxl检查当前日期是否介于两者之间。有人能告诉我怎么做吗?在

我很难将日期转换成一个可以操作的变量。当我说这句话时:

B2包含日期

日期=打印(工作表范围[“B2”].值)

日期等于“无”

所以任何帮助都会很棒的谢谢。在

  ###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')

Tags: fromimportifisvaluematchthisb2