题主一直有写计划的习惯,每天早上第一件事情就是先把每日计划好,不写计划,感觉一天里的生活毫无头绪。由于最近刚刚入门python,遂涌出一股用python来写每日计划的冲动,先上一下效果图。
1. 每日计划记录
2. 每日计划检验与汇总
以上内容都是通过代码自动生成,只需要简单写一写每日任务,美丽的表格自动生成出来。那么该如何去实现呢,首先,我们从要实现的功能入手。
我首先分析了一下所要实现的计划需要具备什么功能,大概如下:
有了下列功能 ,那么开始分步骤实现吧~
对于时间和天气,肯定不能用手机查完后手动输入,那未免也太麻烦了。
1. 获取时间
可以调用time模块的strftime()功能,我采用的是【月-日-星期】的格式,你也可以根据自身喜好更换。
import time
date = time.strftime("%m-%d-%A")
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
2. 获取当地的气温是利用爬虫爬今日天气
这种代码上网比比皆是,拿来直接就用。
import requests,json
def getWhether(city,link):
list = []
url = link + city
r = requests.get(url).json()
whether = r['data']['forecast'][0]['type']
return whether
def getTemprature1(city,link):
list = []
url = link + city
r = requests.get(url).json()
tempreture_high =r['data']['forecast'][0]['high']
return tempreture_high
def getTemprature2(city,link):
list = []
url = link + city
r = requests.get(url).json()
tempreture_low =r['data']['forecast'][0]['low']
return tempreture_low
data = {
'link':'http://open.iciba.com/dsapi/',
'link2':'http://wthrcdn.etouch.cn/weather_mini?city=',
'city':'**',
}
whether = getWhether(data['city'],data['link2'])
temprature1 = getTemprature1(data['city'],data['link2'])
temprature2 = getTemprature2(data['city'],data['link2'])
3. 写入每日计划
由于我是采用openpyxl库对xlsx文件进行的操作(因为想要更方便的对单元格填充颜色),一个很简单的操作。由于计划检验需要先把计划写到excel表格中,所以我把这个分为两个py程序去写。
from openpyxl import load_workbook
excel_address = r"C:\Users\Administrator\Desktop\plan.xlsx"
workbook = load_workbook(excel_address)
sheet = workbook.worksheets[0]
def make_plan(daili_time):
"""制定一个制定计划的函数"""
sheet["A1"] = "日期"
sheet["B1"] = time.strftime("%Y/%m/%d")
sheet["A2"] = "天气"
sheet["B2"] = str(whether)
sheet["A3"] = "温度"
sheet["B3"] = str(temprature1)
sheet["C3"] = str(temprature2)
sheet["A4"] = "时间安排"
sheet["B4"] = "计划详情"
sheet["C4"] = "完成情况"
sheet["A12"] = "对今天的评价"
sheet["B12"] = ""
plans = sheet['B5':'B11']
i = 0
plans = []
for ti in daili_time:
i += 1
msg = input(str(i) + "." + ti + ":")
plans.append(msg)
sheet.cell(i+4, 1).value = ti
sheet.cell(i+4, 2).value = plans[i-1]
sheet.cell(i+4,3).value = ''
workbook.save(excel_address)
workbook.close()
plan = ['9:00-10:00','10:00-12:00']
make_plan(plan)
4. 每日计划检验及汇总
采取的方法大概是提取上述代码生成的内容,例如时间,计划等等,复制到对应的单元格里。具体怎么实现呢?
如下图所示,我将要汇总表里提前写好日期,利用【time.strftime("%m-%d")】与日期匹配,可以找到日期在汇总表里的位置,根据这个位置可以推测出其余内容需要复制到的位置。套用几个for循环就可以实现。
举个例子:首先搜索04-02在汇总表中的位置,返回为Bi,那么时间列的位置就应该是Ci-Ci+6,计划内容列就是Di-Di+6,计划完成情况和其他以此类推。怎么对计划进行检验呢,更简单了,调用input函数就可以了,因为代码行比较多,此处就不展示了。
5. 美工-更改单元格、字体的格式
如果单元格是白底黑字的话未免有点单调,你可以设置一点格式,使计划生成的更美丽。
我在下面列举了十多种比较萌的色系,猛男落泪~~~
font1 = Font(name=u'宋体',size=13,bold=False,italic=False,vertAlign=None,underline='none',
strike=False,color='FF000000')
font2 = Font(name='Times New Roman',size=14,bold=False,italic=False,vertAlign=None,underline='none',
strike=False,color='FF000000')
fill_1 = PatternFill("solid", fgColor='EE5C42')
fill_2 = PatternFill("solid", fgColor='90EE90')
fill_3 = PatternFill("solid", fgColor='9F79EE')
fill_4 = PatternFill("solid", fgColor='FF6EB4')
fill_5 = PatternFill("solid", fgColor='FFD700')
fill_6 = PatternFill("solid", fgColor='B0E2FF')
fill_7 = PatternFill("solid", fgColor='C1FFC1')
fill_8 = PatternFill("solid", fgColor='BCD2EE')
fill_9 = PatternFill("solid", fgColor='B03060')
fill_10 = PatternFill("solid", fgColor='9F79EE')
fill_11 = PatternFill("solid", fgColor='9F79EE')
fill_12 = PatternFill("solid", fgColor='48D1CC')
fill_13 = PatternFill("solid", fgColor='EEEE00')
fill_14 = PatternFill("solid", fgColor='8B5F65')
在下是python新手,所以有点代码没有优化,看起来很繁琐,如若有大神喜欢,请帮忙优化。
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, colors,Font
import requests,json
import time
def getWhether(city,link):
list = []
url = link + city
r = requests.get(url).json()
whether = r['data']['forecast'][0]['type']
return whether
def getTemprature1(city,link):
list = []
url = link + city
r = requests.get(url).json()
tempreture_high =r['data']['forecast'][0]['high']
return tempreture_high
def getTemprature2(city,link):
list = []
url = link + city
r = requests.get(url).json()
tempreture_low =r['data']['forecast'][0]['low']
return tempreture_low
data = {
'link':'http://open.iciba.com/dsapi/',
'link2':'http://wthrcdn.etouch.cn/weather_mini?city=',
'city':'菏泽',
}
whether = getWhether(data['city'],data['link2'])
temprature1 = getTemprature1(data['city'],data['link2'])
temprature2 = getTemprature2(data['city'],data['link2'])
excel_address = r"C:\Users\Administrator\Desktop\plan\2020年每日计划.xlsx"
workbook = load_workbook(excel_address)
sheet = workbook.worksheets[0]
font1 = Font(name=u'宋体',size=13,bold=False,italic=False,vertAlign=None,underline='none',strike=False,color='FF000000')
font2 = Font(name='Times New Roman',size=14,bold=False,italic=False,vertAlign=None,underline='none',strike=False,color='FF000000')
font3 = Font(name='Times New Roman',size=13,bold=False,italic=False,vertAlign=None,underline='none',strike=False,color='FF000000')
def make_plan(daili_time):
"""制定一个制定计划的函数"""
sheet["A1"] = "日期"
sheet["B1"] = time.strftime("%Y/%m/%d")
sheet["A2"] = "天气"
sheet["B2"] = str(whether)
sheet["A3"] = "温度"
sheet["B3"] = str(temprature1)
sheet["C3"] = str(temprature2)
sheet["A4"] = "时间安排"
sheet["B4"] = "计划详情"
sheet["C4"] = "完成情况"
sheet["A12"] = "对今天的评价"
sheet["B12"] = ""
plans = sheet['B5':'B11']
i = 0
fill_title = PatternFill("solid", fgColor='CDC9C9')
fill_date = PatternFill("solid", fgColor='FFF68F')
fill_whether = PatternFill("solid", fgColor='E066FF')
fill_th = PatternFill("solid", fgColor='EE5C42')
fill_tl = PatternFill("solid", fgColor='87CEFF')
fill_time = PatternFill("solid", fgColor='EEA9B8')
fill_plan = PatternFill("solid", fgColor='B0E2FF')
fill_situation = PatternFill("solid", fgColor='C1FFC1')
fill_judge = PatternFill("solid", fgColor='D1EEEE')
plans = []
for ti in daili_time:
i += 1
msg = input(str(i) + "." + ti + ":")
plans.append(msg)
sheet.cell(i+4, 1).value = ti
sheet.cell(i+4, 1).fill = fill_time
sheet.cell(i+4, 1).font = font2
sheet.cell(i+4, 2).value = plans[i-1]
sheet.cell(i+4,3).value = ''
sheet.cell(i+4, 2).fill = fill_plan
sheet.cell(i + 4, 2).font =font3
sheet.cell(i + 4, 3).fill = fill_situation
sheet.merge_cells('B1:C1')
sheet.merge_cells('B2:C2')
sheet.merge_cells('B12:C12')
sheet["B1"].fill = fill_date
sheet["B1"].font = font1
sheet["B2"].fill = fill_whether
sheet["B2"].font = font1
sheet["B3"].fill = fill_th
sheet["B3"].font = font1
sheet["C3"].fill = fill_tl
sheet["C3"].font = font1
#对固定的标题进行编辑
sheet["A1"].fill = fill_title
sheet["A1"].font = font1
sheet["A2"].fill = fill_title
sheet["A2"].font = font1
sheet["A3"].fill = fill_title
sheet["A3"].font = font1
sheet["A4"].fill = fill_title
sheet["A4"].font = font1
sheet["B4"].fill = fill_title
sheet["B4"].font = font1
sheet["C4"].fill = fill_title
sheet["C4"].font = font1
sheet["A12"].fill = fill_title
sheet["A12"].font = font1
sheet["B12"].fill = fill_judge
sheet["B12"].font = font3
title = time.strftime("%m-%d-%A")
sheet.title = str(title)
workbook.save(excel_address)
workbook.close()
print("\n今天的天气是:"+whether+"\n今天的温度是:"+temprature1+"-"+\
temprature2+"\nHave a nice day~~")
answer = input("请按随意键结束程序~")
plan = ['9:00-10:00','10:00-12:00','1:30-3:00', '3:00-5:00', '5:00-6:00',
'7:30-8:30', '8:30-10:00']
if __name__ == "__main__":
try:
make_plan(plan)
except PermissionError:
print("\n【警告】:Excel软件已经打开,请关闭软件后再运行程序!\n")
quiry = input("你想再次运行程序吗?(y/n):")
if quiry == "y":
main()
else:
a = input("请按任意键关闭程序")
from openpyxl import *
from copy import copy
from openpyxl.styles import PatternFill, colors,Font,Alignment
import time
from random import choice
def main():
excel_address_1 = r"C:\Users\Administrator\Desktop\plan\2020年每日计划.xlsx"
workbook = load_workbook(excel_address_1)
sheet = workbook.worksheets[0]
sheet2 = workbook["每日计划情况汇总"]
fill = []
fill_1 = PatternFill("solid", fgColor='EE5C42')
fill_2 = PatternFill("solid", fgColor='90EE90')
fill_3 = PatternFill("solid", fgColor='9F79EE')
fill_4 = PatternFill("solid", fgColor='FF6EB4')
fill_5 = PatternFill("solid", fgColor='FFD700')
fill_6 = PatternFill("solid", fgColor='B0E2FF')
fill_7 = PatternFill("solid", fgColor='C1FFC1')
fill_8 = PatternFill("solid", fgColor='BCD2EE')
fill_9 = PatternFill("solid", fgColor='B03060')
fill_10 = PatternFill("solid", fgColor='9F79EE')
fill_11 = PatternFill("solid", fgColor='9F79EE')
fill_12 = PatternFill("solid", fgColor='48D1CC')
fill_13 = PatternFill("solid", fgColor='EEEE00')
fill_14 = PatternFill("solid", fgColor='8B5F65')
fill.append(fill_4)
fill.append(fill_5)
fill.append(fill_6)
fill.append(fill_7)
fill.append(fill_8)
fill.append(fill_9)
fill.append(fill_10)
fill.append(fill_11)
fill.append(fill_12)
fill.append(fill_13)
fill.append(fill_14)
font1 = Font(name='Times New Roman',size=13,bold=False,italic=False,\
vertAlign=None,underline='none',strike=False,color='FF000000')
font2 = Font(name=u'宋体',size=14,bold=False,italic=False,vertAlign=None,\
underline='none',strike=False,color='FF000000')
font3 = Font(name=u'宋体',size=26,bold=False,italic=False,
vertAlign=None,underline='none',strike=False,color='FF000000')
k = 0
times = []
plans = []
situations = []
for i in range(7):
i += 1
clock = str(sheet.cell(i+4,1).value)
plan = str(sheet.cell(i+4,2).value)
times.append(clock)
plans.append(plan)
answer = input(str(i)+"."+str(sheet.cell(i+4,1).value)+":\
"+str(sheet.cell(i+4,2).value)+ "\t\n此项计划的完成情况:")
if answer =="1":
sheet.cell(i + 4, 3).value = "完成"
sheet.cell(i + 4, 3).fill = fill_2
sheet.cell(i + 4, 3).font = font1
k += 1
situations.append(str(sheet.cell(i + 4, 3).value))
elif answer == "2":
sheet.cell(i + 4, 3).value = "未完成"
sheet.cell(i + 4, 3).fill = fill_1
sheet.cell(i + 4, 3).font = font1
situations.append(str(sheet.cell(i + 4, 3).value))
else:
explain = input("此项计划没有完成的原因:")
sheet.cell(i + 4, 3).value = explain
sheet.cell(i + 4, 3).fill = fill_3
sheet.cell(i + 4, 3).font = font1
situations.append(str(sheet.cell(i + 4, 3).value))
k += 1
if k < 4:
score = "真是个臭弟弟,计划完成的不达标!!!"
sheet["B12"].fill = fill_1
elif k >= 6:
score = "真是太棒了,完美的完成了任务~"
sheet["B12"].fill = fill_2
elif 4<= k <6:
score = "反应平平,计划完成的一般。"
sheet["B12"].fill = fill_3
for r in range(1, sheet2.max_row+1):
for c in range(1, sheet2.max_column+1):
value = sheet2.cell(r, c).value
if value == str(time.strftime("%m-%d")):
if k<4:
sheet2.cell(r,c+4).value = "☆"
sheet2.cell(r,c+4).fill = fill_1
sheet2.cell(r,c+4).font = font3
elif k >= 6:
sheet2.cell(r,c+4).value = "★"
sheet2.cell(r,c+4).font = font3
sheet2.cell(r,c+4).fill = fill_2
elif 4<= k <6:
sheet2.cell(r,c+4).value = "●_●"
sheet2.cell(r,c+4).fill = fill_3
sheet2.cell(r,c+4).font = font2
m = choice([0,1,2,3,4,5,6,7,8,9,10])
for i in range(7):
sheet2.cell(r, c+1).value = times[i]
sheet2.cell(r, c+1).fill = fill[m]
sheet2.cell(r, c+2).value = plans[i]
sheet2.cell(r, c+2).fill = fill[m]
sheet2.cell(r, c+3).value = situations[i]
if sheet2.cell(r, c+3).value == "未完成":
sheet2.cell(r, c+3).fill = fill_1
elif sheet2.cell(r, c+3).value == "完成":
sheet2.cell(r, c+3).fill = fill_2
else:
sheet2.cell(r, c+3).fill = fill_3
r +=1
judge = input("你今天做出了哪些突破:")
sheet["B12"] = score +" \
今天的突破:【"+judge+"】"
copy_sheet1 = workbook.copy_worksheet(workbook.worksheets[0])
copy_sheet1.title = str(time.strftime("%m-%d"))
workbook.save(excel_address_1)
print("今天你完成了{}项任务,今天的评价是:{}".format(k,score))
ask = input("请按随意键关闭程序!")
if __name__ == "__main__":
try:
main()
except PermissionError:
print("\n【警告】:Excel软件已经打开,请关闭软件后再运行程序!\n")
quiry = input("你想再次运行程序吗?(y/n):")
if quiry == "y":
main()
else:
a = input("请按任意键关闭程序")