【发布时间】:2015-06-20 17:14:25
【问题描述】:
您好,我正在尝试创建一个 python 程序,该程序使用 openpyxl 来验证 excel 表中的两个单元格 (1)start_date 和 (2)end_date,以检查 current_date 是否介于两者之间。有人对我如何做到这一点有任何指示吗?
我无法将日期转换为我可以操作的变量。当我把这行:
B2 包含一个日期
日期 = 打印(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')
【问题讨论】:
-
没有更多信息无法回答这个问题。
sheet_ranges是什么?您对哪些细胞感兴趣? -
sheet_ranges 只是 Excel 工作表的名称。
-
请包含更多代码(检查格式指南)以及您感兴趣的单元格。
-
好的,我们就说'B2'
-
根据其他答案
date = print(…)将始终为无。如果 ws['B2'] 具有正确的数字格式,它将被转换为 Python 日期时间。您可以拨打cell.is_date查询。