想打印日历,但有些是要钱的,所以我用python...
算是突然想打印日历,在网上搜不到想要的,又想练一下手,所以有以下代码#例1;
#农历大小月的天数列表 实时修改
# 11 12 12345678910 11 12 1 多些月份没关系,后面是有可能用到的
nong_li_ds =
#农历月份多一些是因为阳历和农历不一致,并考虑到有闰月
nong_li_yue=["十一月","十二月","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月","正月"]
#按1月1号的农历号减一
cx = 12
# count 计数周几,1月1号是星期四,周一开始算,所以是第四4天,0,1,2,3
count = 3
#年份
year = 2026
# 以上参数要按实际修改,所有写这个日历的代码是为什么呢……………………。
#阳历的天数
days =[]
#农历的天数
nong_li_days =[]
nong_li = [" ",'初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十',
'十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九','廿十',
'廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十']
# 判断year是平年还是闰年
def is_leap_year(year):
# 如果年份能被4整除且不能被100整除,或者能被400整除,则是闰年
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
# 闰年的天数要在这里修改
if is_leap_year(year):
ping_nian =
else:
ping_nian =
# 新历列表添加项目函数
def listes_day(yue_di):
for i in range(1,yue_di):
days.append(i)
# 农历列表添加项目函数
def listes_nong_li_day(yue_di):
for i in range(1,yue_di):
nong_li_days.append(nong_li)
'''
def get_days(months):
for i in range(len(months)):
listes_day(months+1)
def get_nong_li_days(months):
for i in range(len(months)):
listes_nong_li_day(months+1)
'''
# 按每月的天数添加日子
def get_days_hs(months,hs):
for i in range(len(months)):
hs(months+1)
get_days_hs(ping_nian,listes_day)
get_days_hs(nong_li_ds,listes_nong_li_day)
#把农历每月的初一改成当月的月份
count1 = 0
for i in range(len(nong_li_days)):
if nong_li_days == "初一":
nong_li_days = nong_li_yue
count1 += 1
# 闰年378天,12按1月1号的农历号减一
nong_li_days = nong_li_days
#print(nong_li_days)
#print(len(nong_li_days))
#print(days)
#print(len(days))
# 这个随便抄的AI的 ,对css不熟。
text = """
<!DOCTYPE html>
<html lang="zh-CN">
<head> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{year}年日历表</title>
<style>body{ font-family: 'Microsoft YaHei', sans-serif; line-height: 1.6; margin: 20px; background-color: #f9f9f9; color: #333; }
.container { max-width: 1200px; margin: 0 auto; background-color: #fff; padding: 30px; border-radius: 10px;box-shadow: 0 2px 15px rgba(0,0,0,0.1); }
h1 { text-align: center; color: #2c3e50; margin-bottom: 10px; border-bottom: 2px solid #3498db; padding-bottom: 15px; }
.subtitle { text-align: center;color: #7f8c8d; margin-bottom: 30px; font-size: 1.1em; }
.year-nav { text-align: center; margin-bottom: 20px; font-size: 1.2em; }
.year-nav a { text-decoration: none; color: #3498db; margin: 0 15px; padding: 5px 10px;border: 1px solid #ddd; border-radius: 4px; }
.year-nav a:hover { background-color: #f0f0f0; }
.month-table { width: 100%; border-collapse: collapse; margin-bottom: 40px; page-break-inside: avoid; }
.month-table caption { caption-side: top; font-size: 1.5em;font-weight: bold; padding: 15px;
background-color: #3498db; color: white; border-radius: 5px 5px 0 0;}
.month-table th, .month-table td { border: 1px solid #ddd; padding: 12px 8px; text-align: center; vertical-align: top; width: 10.28%; }
.month-table th { background-color: #f2f2f2; font-weight: bold; color: #2c3e50; }
.day-cell { min-height: 60px; position: relative; }
.day-num { font-size: 1.2em; font-weight: bold; display: block; margin-bottom: 5px; }
.lunar { font-size: 0.85em; color: #e74c3c; display: block; margin-bottom: 3px; }
.festival, .solar-term { font-size: 0.8em; color: #27ae60; display: block; font-weight: bold; margin-top: 3px; }
.weekend { background-color: #3fcb54; } .rest-day { color: #e74c3c; font-weight: bold; }
.work-day { color: #f39c12; font-weight: bold; }
.download-section { text-align: center; margin-top: 40px;padding-top: 20px; border-top: 1px dashed #ccc; }
.download-btn { display: inline-block; background-color: #2ecc71;color: white; padding: 12px 25px; margin: 10px;
text-decoration: none; border-radius: 5px; font-weight: bold; }
.download-btn:hover{ background-color: #27ae60; }
.note { font-size: 0.9em; color: #95a5a6; text-align: center; margin-top: 10px; }
@media print {
body { background-color: white; margin: 0; padding: 0; }
.container { box-shadow: none; padding: 0; }
.download-section, .year-nav { display: none;} }
</style> </head> <body>
"""
#print(nong_li_days)
#print(len(nong_li_days))
#print(days)
#print(len(days))
day_i = 0
for i in days:
if count % 7 == 0 and i != 1 :# 每月月底多了一个空白周
text += f'</tr><tr><td >{(count//7)+1}</td>'
if i == 1:
text += f"""<table class="month-table">
<caption>{year}年{(day_i//30)+1}月</caption>
<thead>
<tr>
<th>周</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th>
</tr>
</thead><tbody>""" # 每月1号前换行
text +=f"<tr><td>{(count//7)+1}</td>"
# 问题:结尾的时候,多了一周显示 如果最后一天是周日
for a in range(count%7):#1号前补空格填满一周
text +="<td></td>"
text += f'<td class="day-cell"><span class="day-num">{i}</span><span class="lunar">{nong_li_days}</span></td>'# 每日的日
if i >= 28:
try:
if days==1: #月的最后一天补空格填满一周
for a in range(6-count%7):
text +="<td></td>"
except:#年的最后一天补空格填满一周
for a in range(6-count%7):
text +="<td></td>"
day_i+=1
count+=1
text += """ </tr>
</tbody>
</table>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
});
</script>
</body> </html>"""
#print(text)
#写入文件
filename = f"{year}年日历.html"
#with open("2026.html",'w',encoding='utf-8')as f:
with open(filename,'w',encoding='utf-8')as f:
f.write(text)
当做一个记录吧 建议补个说明文档
页:
[1]