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
- 下一篇: conda虚拟环境中安装ipython