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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值