python生成日历_使用Python实现简易月历生成(2)
生成日历主程序的函数结构和注释:#此功能用于在place处追加字体为f_name,字号为f_size,内容为content的细/粗体字
def convert_text(place, f_name, f_size, content, f_bold):
#此功能用于输出以date为首的四周月历,以及判断是否跨年并改年份
def get_month_info(date_, yr_):
#此函数用于将代表周几的数字转换为字符串,方便改日历语言
def convert_num_weekday(_):
#此函数用于将代表月份的数字转换为字符串,方便改日历语言
def convert_num_month(_):
#输出+输入函数
def inp(_):
#判断该日是否为周一
def judge_monday(f_date):
#主函数
def main():
if __name__ == '__main__':
main()
完整代码:
# -*- coding: utf-8 -*-
import docx
import json
#此功能用于在place处追加字体为f_name,字号为f_size,内容为content的细/粗体字
def convert_text(place, f_name, f_size, content, f_bold):
run = place.add_run(content)
run.font.name = f_name
run.font.size = f_size
run.font.bold = f_bold
#此功能用于输出以date为首的四周月历,以及判断是否跨年并改年份
def get_month_info(date_, yr_):
#载入完整日历
with open('year_calendar.json', 'r') as f:
y_cal = json.loads(f.read())
#读取外部年份列表地址下的年份信息
yr = yr_[0]
date = date_.rsplit('/',1)[0]
title = 'Schedule of ' + convert_num_month(date.split('/')[1]) + '-' + str(yr)
#遍历该年每一周的每一日,确定date在该年哪一周,周数为w
for week in y_cal[str(yr)]:
for day in y_cal[str(yr)][week]:
if day == date:
w = week
#定义列表mon_info用于储存四周信息
mon_info = []
#定义range_用于i的叠加
range_ = range(int(w), int(w)+5)
for i in range_:
#确认i是否越界,防止年末日历年周数指数越界
#如果未越界
if i <= len(y_cal[str(yr)]):
mon_info.append(y_cal[str(yr)][str(i)])
#如果刚越界一周,并且上一年的年末周存在补全的''
elif i == len(y_cal[str(yr)]) + 1 and '' in mon_info[len(mon_info)-1][6]:
#判断越界第一周的年末数据第几位为''
for j in range(7):
if mon_info[len(mon_info) - 1][j]=='':
break
#判断越界时,前一年的日期个数是否大于14
if int(j + (i - int(w) - 1) * 7) < 14:
print '1'
#如果不是,则标题为后一年1月日历
title = 'Schedule of ' + convert_num_month(1) + '-' + str(int(yr)+1)
#将新年第一周的值覆盖年末空值并加上月份尾数/1
for k in range (j, 7):
mon_info[len(mon_info) - 1][k] = str(k-j+1) + '/1'
#改外部列表地址下的年份(改动会影响外部数据值)
yr_[0] = str(int(yr_[0])+1)
#扩充一次循环,以弥补合并的年底、年初两周数据
range_.append(int(w)+6)
#如果越界超过一周
else:
mon_info.append(y_cal[str(int(yr)+1)][str(i-len(y_cal[str(yr)]))])
print 'mon_info = ', mon_info
print 'title = ', title
return mon_info, title
#此函数用于将代表周几的数字转换为字符串,方便改日历语言
def convert_num_weekday(_):
if _ == 0 or _ == '0':
return 'Monday'
if _ == 1 or _ == '1':
return 'Tuesday'
if _ == 2 or _ == '2':
return 'Wednesday'
if _ == 3 or _ == '3':
return 'Thursday'
if _ == 4 or _ == '4':
return 'Friday'
if _ == 5 or _ == '5':
return 'Saturday'
if _ == 6 or _ == '6':
return 'Sunday'
#此函数用于将代表月份的数字转换为字符串,方便改日历语言
def convert_num_month(_):
if _ == 1 or _ == '1':
return 'January'
if _ == 2 or _ == '2':
return 'February'
if _ == 3 or _ == '3':
return 'March'
if _ == 4 or _ == '4':
return 'April'
if _ == 5 or _ == '5':
return 'May'
if _ == 6 or _ == '6':
return 'June'
if _ == 7 or _ == '7':
return 'July'
if _ == 8 or _ == '8':
return 'August'
if _ == 9 or _ == '9':
return 'September'
if _ == 10 or _ == '10':
return 'October'
if _ == 11 or _ == '11':
return 'November'
if _ == 12 or _ == '12':
return 'December'
#输出+输入函数
def inp(_):
print _
return raw_input()
#判断该日是否为周一
def judge_monday(f_date):
import time
date = time.strptime(f_date, '%d/%m/%Y')
if date.tm_wday == 0:
return True
else: return False
#主函数
def main():
calendar_name = 'year_calendar.json'
schedule_name = 'schedule module.docx'
#如果路径不存在json,则创建
import os
if not os.path.exists(calendar_name):
print 'Creating year calendar'
import print_year_calendar
else: print 'Year calendar exists'
#循环输入首日直至正确
while True:
first_date = inp('The first date in calendar. Eg: 4/6/2018')
#判断首日是否为周一
if judge_monday(first_date):
break
else: print 'Wrong date, input again.'
year = []
year.append(first_date.split('/')[2])
#读取docx模板,需要在interpreter中加入python-docx
doc = docx.Document(schedule_name)
# 读取json日历(在get_month_info()中完成)
month_info, title_info = get_month_info(first_date,year)
table = doc.tables[0]
#使用5*7循环将此表格的每一项填入表中
for row in range(5):
for col in range(7):
if row == 0:
#若是第一行,则此行每列分别填入星期几
convert_text(table.cell(row,col).paragraphs[0],'Calibri', 150000, convert_num_weekday(col), False)
else:
#若非,则正常输入日历
convert_text(table.cell(row, col).paragraphs[0], 'Calibri', 140000, month_info[row-1][col], False)
#加入标题
convert_text(doc.paragraphs[0], 'Calibri', 180000, ' ' + title_info.split(' ',1)[1], True)
#保存为
doc.save(title_info + '.docx')
if __name__ == '__main__':
main()
python生成日历_使用Python实现简易月历生成(2)相关推荐
- python生成日历_使用python生成ical日历文件——将课程表导入到手机日历
1 importdatetime,os2 3 classEvent:4 """ 5 事件对象6 """ 7 def __init__(sel ...
- python 概率分布模型_使用python的概率模型进行公司估值
python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...
- python 时间序列预测_使用Python进行动手时间序列预测
python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...
- python打印一年的日历_使用Python打印日历
在本教程中,我们将学习如何使用Python的日历模块打印月份和年份的日历.在Python中这很简单.我们需要年和月的数字.而已. 让我们看看如何打印年历.请按照以下步骤打印年历.导入日历模块. 初始化 ...
- python加法例子_用python给小孩随机生成一组10以内加减法
一.案例背景:同事家小孩子要上小学了,准备给小孩练习10以内加减法,为了解决出题的烦恼,希望我用python帮他实现,并保存到word里.近期在学习python,就当练手了,以后我们家娃也能用.代码简 ...
- python集群_使用Python集群文档
python集群 Natural Language Processing has made huge advancements in the last years. Currently, variou ...
- python 网页编程_通过Python编程检索网页
python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...
- python机器学习预测_使用Python和机器学习预测未来的股市趋势
python机器学习预测 Note from Towards Data Science's editors: While we allow independent authors to publish ...
- python高斯求和_利用Python进行数据分析(3)- 列表、元组、字典、集合
本文主要是对Python的数据结构进行了一个总结,常见的数据结构包含:列表list.元组tuple.字典dict和集合set. image 索引 左边0开始,右边-1开始 通过index()函数查看索 ...
最新文章
- 有一份华为荣誉证书等你来拿!网络人工智能硬盘异常预测黑客松比赛火热报名中...
- hibernate session 新增、删除,修改、查询样例
- ERP项目需要持续的呵护
- PHP $_SERVER['HTTP_REFERER'] 获取前一页面的 URL 地址
- ElasticSearch的搜索推荐(typeahead)
- (23)ISE14.7 PLL输出时钟不能直接输出到普通IO(FPGA不积跬步101)
- popwindow弹窗
- Elasticsearch--进阶-filter过滤---全文检索引擎ElasticSearch工作笔记014
- android4.0自定义锁屏总结【android锁屏研究一】
- 如何用css显示一个图片中多个小图标
- jquery隐式迭代
- HTML5中的一些新特性
- linux内核实时补丁的实现,Canonical释出用于Ubuntu 18.04 LTS和16.04 LTS的新Linux内核实时补丁...
- 有用的在线图片处理网站(证件照替换背景蓝色)
- 用C语言多线程描述哲学家,并行程序设计-哲学家就餐问题
- 05月12日三支有望飙涨股与操盘策略分析
- docker启动容器指定主机名,网络和ip地址
- 蓝桥杯 历届试题 小朋友排队 C++
- Datawhale组队学习周报(第022周)
- 【SF08】:经典KD指标另类使用有奇效,股指商品双版本策略;