日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python生成日历_使用Python实现简易月历生成(2)

發(fā)布時間:2023/12/10 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python生成日历_使用Python实现简易月历生成(2) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

生成日歷主程序的函數(shù)結(jié)構(gòu)和注釋:#此功能用于在place處追加字體為f_name,字號為f_size,內(nèi)容為content的細/粗體字

def convert_text(place, f_name, f_size, content, f_bold):

#此功能用于輸出以date為首的四周月歷,以及判斷是否跨年并改年份

def get_month_info(date_, yr_):

#此函數(shù)用于將代表周幾的數(shù)字轉(zhuǎn)換為字符串,方便改日歷語言

def convert_num_weekday(_):

#此函數(shù)用于將代表月份的數(shù)字轉(zhuǎn)換為字符串,方便改日歷語言

def convert_num_month(_):

#輸出+輸入函數(shù)

def inp(_):

#判斷該日是否為周一

def judge_monday(f_date):

#主函數(shù)

def main():

if __name__ == '__main__':

main()

完整代碼:

# -*- coding: utf-8 -*-

import docx

import json

#此功能用于在place處追加字體為f_name,字號為f_size,內(nèi)容為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在該年哪一周,周數(shù)為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是否越界,防止年末日歷年周數(shù)指數(shù)越界

#如果未越界

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]:

#判斷越界第一周的年末數(shù)據(jù)第幾位為''

for j in range(7):

if mon_info[len(mon_info) - 1][j]=='':

break

#判斷越界時,前一年的日期個數(shù)是否大于14

if int(j + (i - int(w) - 1) * 7) < 14:

print '1'

#如果不是,則標題為后一年1月日歷

title = 'Schedule of ' + convert_num_month(1) + '-' + str(int(yr)+1)

#將新年第一周的值覆蓋年末空值并加上月份尾數(shù)/1

for k in range (j, 7):

mon_info[len(mon_info) - 1][k] = str(k-j+1) + '/1'

#改外部列表地址下的年份(改動會影響外部數(shù)據(jù)值)

yr_[0] = str(int(yr_[0])+1)

#擴充一次循環(huán),以彌補合并的年底、年初兩周數(shù)據(jù)

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

#此函數(shù)用于將代表周幾的數(shù)字轉(zhuǎn)換為字符串,方便改日歷語言

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'

#此函數(shù)用于將代表月份的數(shù)字轉(zhuǎn)換為字符串,方便改日歷語言

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'

#輸出+輸入函數(shù)

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

#主函數(shù)

def main():

calendar_name = 'year_calendar.json'

schedule_name = 'schedule module.docx'

#如果路徑不存在json,則創(chuàng)建

import os

if not os.path.exists(calendar_name):

print 'Creating year calendar'

import print_year_calendar

else: print 'Year calendar exists'

#循環(huán)輸入首日直至正確

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循環(huán)將此表格的每一項填入表中

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

總結(jié)

以上是生活随笔為你收集整理的python生成日历_使用Python实现简易月历生成(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。