日历【参考网上的一个模板】

def leap_year(year):  # 判断平润年
    if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
        return True
    else:
        return False


def get_month_days(year, month):  # 得到每个年份每月的天数
    days = 31
    if month == 2:
        if leap_year(year):
            days = 29
        else:
            days = 28
    elif month == 4 or month == 6 or month == 9 or month == 11:
        days = 30
    return days


def get_total_days(year, month):  # 计算星期
    total_days = 0
    for y in range(1900, year):
        if leap_year(y):
            total_days += 366
        else:
            total_days += 365
    for m in range(1, month):
        total_days += get_month_days(year, m)
    return total_days
if __name__ == "__main__":
    inp_year = int(input("输入年份:"))
    inp_month = int(input("请输入月:"))
    if inp_month not in range(1, 13):
        print("输入月份有误!")
    else:
        print("日\t一\t二\t三\t四\t五\t六")
        iCount = 0
        week_bagin = (get_total_days(inp_year, inp_month) % 7) + 1
        month_days = get_month_days(inp_year, inp_month)+1
        for i in range(week_bagin):
            if week_bagin == 7:
                break
            else:
                print("\t", end="")
                iCount += 1
        for i in range(1, month_days):
            iCount += 1
            print(i, end="\t")
            if iCount % 7 == 0:
                print("")

"""
简洁版:
import calendar
year = int(input("输入年份:"))
month = int(input("输入月份:"))
if month not in range(1, 13):
print("输入月份有误")
else:
    cal = calendar.month(year,month)
    print(cal)
"""
"""
import calendar
while True:
    print("输入0退出程序")
    year = int(input("输入年份:"))
    if year == 0:
        break
    month = int(input("输入月份:"))
    if month == 0:
        break
    if month in range(1, 13):
        cal = calendar.month(year, month)
        print(cal)
    else:
        print("输入月份有误!")
 """
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮您实现这个功能。首先,我们需要安装相关的库,包括`django`和`pillow`。 在安装好这两个库之后,我们可以创建一个新的django项目,命名为`calendar_2023`。然后,我们可以在项目中创建一个新的应用,命名为`calendar`。 接下来,我们需要定义一个视图函数来生成日历。我们可以在`views.py`文件中定义一个函数,例如`generate_calendar(request)`。这个函数的作用是生成2023年的日历,然后将其转换为图形模板并返回给浏览器。 具体实现可以参考下面的代码: ```python from django.shortcuts import render from django.http import HttpResponse from PIL import Image, ImageDraw, ImageFont import calendar def generate_calendar(request): # 设置图片大小 width, height = 800, 600 # 创建一张空白图片 img = Image.new('RGB', (width, height), color='white') # 创建画布 draw = ImageDraw.Draw(img) # 设置字体 font = ImageFont.truetype('arial.ttf', size=36) # 绘制标题 title = '2023年' title_width, title_height = draw.textsize(title, font=font) title_x = (width - title_width) // 2 title_y = 50 draw.text((title_x, title_y), title, font=font, fill='black') # 绘制月份 month_width, month_height = draw.textsize('January', font=font) cell_width, cell_height = (width - 100) // 7, (height - 200) // 6 x, y = 50, 150 for month in range(1, 13): month_name = calendar.month_name[month] draw.text((x, y), month_name, font=font, fill='black') x += cell_width if x >= width - cell_width: x = 50 y += cell_height + month_height # 保存图片 img.save('calendar.png') # 读取图片并返回给浏览器 with open('calendar.png', 'rb') as f: response = HttpResponse(f.read(), content_type='image/png') response['Content-Disposition'] = 'attachment; filename="calendar.png"' return response ``` 在这个函数中,我们使用了`Pillow`库来创建一张空白的图片,并使用`ImageDraw`模块来进行绘制。首先,我们绘制了标题“2023年”,然后在图片中绘制了12个月份的名称。最后,我们保存这张图片并将其返回给浏览器。 在`urls.py`文件中,我们需要定义一个URL模式来映射到这个视图函数。例如: ```python from django.urls import path from . import views urlpatterns = [ path('generate_calendar/', views.generate_calendar, name='generate_calendar'), ] ``` 现在,我们可以运行django服务器,并在浏览器中访问`http://localhost:8000/generate_calendar/`来生成并下载2023年的日历图形模板
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅帅的Python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值