K.I.S.S---Keep IT Simple,Stupid!    人生苦短,我用Python

Python 打印日历

 
分类: 问答 2024年7月25日

import calendar
import datetime
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont

def print_month(year):
    """Prints the month calander based on the current 'year' input.
        Args:
            year: An  integer representing the year.               
    """
  # ... rest of the function code ...

    #holidays is a list which each item is tuple
    # Each item(tuple(holiday date as a string, is_subsidute_working_day, is_normal_holiday, is_public_holiday))
    #holidays[i][1]=0, holiday
    #holidays[i][1]=1, subsidute working day,marked by blue square
    #holidays[i][2]=0, if holidays[i][1]=0, it is normal holiday, marked by red color circle
    #holidays[i][2]=1, if holidays[i][1]=0, it is public holiday, three times salary in China.marked by red color circle+triangle
    holidays=[
        (str(year)+'-1-1',0,1,'New Year\'s Day '),
        (str(year)+'-2-4',1,1,' '),
        (str(year)+'-2-10',0,1,'Chinese New Year '),
        (str(year)+'-2-11',0,1,'Chinese New Year '),
        (str(year)+'-2-12',0,1,'Chinese New Year '),
        (str(year)+'-2-13',0,0,'Chinese New Year '),
        (str(year)+'-2-14',0,0,'Chinese New Year '),
        (str(year)+'-2-15',0,0,'Chinese New Year '),
        (str(year)+'-2-16',0,0,'Chinese New Year '),
        (str(year)+'-2-17',0,0,'Chinese New Year '),
        (str(year)+'-2-18',1,1,' '),
        (str(year)+'-4-4',0,1,'Ching Ming Festival<Tomb Sweeping Day'),
        (str(year)+'-4-5',0,0,'Ching Ming Festival<Tomb Sweeping Day'),
        (str(year)+'-4-6',0,0,'Ching Ming Festival<Tomb Sweeping Day'),
        (str(year)+'-4-7',1,1,' '),
        (str(year)+'-4-28',1,1,' '),
        (str(year)+'-5-1',0,1,'Labor\'s Day'),
        (str(year)+'-5-2',0,0,'Labor\'s Day'),
        (str(year)+'-5-3',0,0,'Labor\'s Day'),
        (str(year)+'-5-4',0,0,'Labor\'s Day'),
        (str(year)+'-5-5',0,0,'Labor\'s Day'),
        (str(year)+'-5-11',1,1,' '),
        (str(year)+'-6-8',0,0,'Dragon-Boat Festival'),
        (str(year)+'-6-9',0,0,'Dragon-Boat Festival'),
        (str(year)+'-6-10',0,1,'Dragon-Boat Festival'),
        (str(year)+'-9-14',1,1,' '),
        (str(year)+'-9-15',0,0,'Mid-Autumn Festival'),
        (str(year)+'-9-16',0,0,'Mid-Autumn Festival'),
        (str(year)+'-9-17',0,1,'Mid-Autumn Festival'),
        (str(year)+'-9-29',1,1,' '),
        (str(year)+'-10-1',0,1,'China National Day'),
        (str(year)+'-10-2',0,1,'China National Day'),
        (str(year)+'-10-3',0,1,'China National Day'),
        (str(year)+'-10-4',0,0,'China National Day'),
        (str(year)+'-10-5',0,0,'China National Day'),
        (str(year)+'-10-6',0,0,'China National Day'),
        (str(year)+'-10-7',0,0,'China National Day'),
        (str(year)+'-10-12',1,1,' '),
        (str(year)+'-12-25',0,3,'Christmas Day, Company Holiday')
    ]


    image_width, image_height = 1280, 1800
    img = Image.new("RGB", (image_width, image_height), color="white")
    draw = ImageDraw.Draw(img)
    # font = ImageFont.load_default()

    font_path = 'msyh.ttc'  # Replace with the actual path to your font file
    font_size = 50
    font_color = 'red'  # You can use color names or hex values (e.g., '#FF0000')
    month_font = ImageFont.truetype(font_path, size=font_size)
    week_font= font = ImageFont.truetype(font_path, 18)
    holiday_font= font = ImageFont.truetype(font_path, 16)
    font = ImageFont.truetype(font_path, 26)
    # Define fill color (red) and outline color (black) with width 3
    fill_color_free = (255, 0, 0)  # green(RGB)
    fill_color_working = (0, 0, 255)  #red(RGB)
    outline_color = (0, 0, 0)  # Black (RGB)
    outline_width = 0

    # Set initial position for drawing
    monthxy=(
        (100,100),(500,100),(900,100),
        (100,500),(500,500),(900,500),
        (100,900),(500,900),(900,900),
        (100,1300),(500,1300),(900,1300)
    )

    x=0
    y=0
    step = 42

    weekdayname=['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
    cal=calendar.Calendar()

    #draw instruction bar
    #--------------------------------------------------------
    barx=x+200
    bary=y+40
    draw.ellipse((barx, bary, barx+30, bary+30), fill=fill_color_free, outline=outline_color, width=outline_width)
    draw.text((barx+40, bary),'Holiday', fill="black", font=font)


    barx=x+500
    draw.ellipse((barx, bary, barx+30, bary+30), fill=fill_color_free, outline=outline_color, width=outline_width)

    x1, y1 =  barx+15,  bary+30
    x2, y2 =  barx+10,  bary+40
    x3, y3 =  barx+20,  bary+40
    draw.polygon([(x1, y1), (x2, y2), (x3, y3)], fill="red")
    draw.text((barx+40, bary),'Public Holiday', fill="black", font=font)

    barx=x+800
    draw.rectangle((barx, bary, barx+30, bary+30), fill=fill_color_working, outline=outline_color, width=outline_width)
    draw.text((barx+40, bary),'Substitute working day', fill="black", font=font)


    #-------------------------------------------------

    for month in range(1, 13):
        dts=cal.itermonthdays2(year,month)
        x=monthxy[month-1][0]
        y=monthxy[month-1][1]
        
        draw.text((x+120, y+20),str(month), fill="black", font=month_font)
        y += 100
        for wd in weekdayname:
           draw.text((x-6, y),str(wd), fill="black", font=week_font)
           x += step
        y += step
        x = monthxy[month-1][0]

        draw.rectangle([x+5*step-6, y-10,x+7*step-6, y+5*step-10], fill='lightgrey')

        holidayname='no'

        for date in dts:
             if date[0] != 0:
                x = monthxy[month-1][0]+date[1]*step
                # draw.text((x, y),str(date[0]), fill="black", font=font)
                for hd in holidays:
                    date_str = hd[0]
                    date_obj = datetime.strptime(date_str, '%Y-%m-%d')
                    if str(month) == str(date_obj.month):
                        if str(date[0]) == str(date_obj.day):
                            # draw.text((x, y),'free', fill="black", font=font)
                            # Define rectangle coordinates (top-left corner, bottom-right corner)
                            xy = (x, y, x+30, y+30)  # (x1, y1, x2, y2)
                            if hd[1] == 0:

                                if holidayname == 'no':
                                    holidayname=hd[3]
                                draw.ellipse(xy, fill=fill_color_free, outline=outline_color, width=outline_width)
                                if hd[2] == 1:
                                    x1, y1 = x+15, y+30
                                    x2, y2 = x+10, y+40
                                    x3, y3 = x+20, y+40
                                    draw.polygon([(x1, y1), (x2, y2), (x3, y3)], fill="red")
                                if hd[2] == 2:
                                    points = [
                                    (x+15, y+step/2),
                                    (x+30, y+30+step/2),
                                    (x,y+10+step/2),
                                    (x+30, y+10+step/2),
                                    (x, y+30+step/2),
                                                ]
                            
                                    # Draw the star outline in red
                                    draw.polygon(points, outline="red")
                                
                                    # Fill the star with red color
                                    draw.polygon(points, fill="red")
                                    
                            else:
                                draw.rectangle(xy, fill=fill_color_working, outline=outline_color, width=outline_width)
          
                if date[0]<10:
                     draw.text((x+step/5, y),str(date[0]), fill="black", font=font)
                else:
                     draw.text((x, y),str(date[0]), fill="black", font=font)
             if date[1] == 6:
                x =monthxy[month-1][0]
                y += step
        if holidayname != 'no':
            x1, y1 = monthxy[month-1][0]+15, y+5
            x2, y2 = monthxy[month-1][0]+10, y+15
            x3, y3 = monthxy[month-1][0]+20, y+15
            if holidayname != 'Christmas Day, Company Holiday':
                draw.polygon([(x1, y1), (x2, y2), (x3, y3)], fill="red")
            draw.text((monthxy[month-1][0]+26, y),holidayname, fill="black", font=holiday_font)
        print('current month is',month)


    # Save the image
    img.save("2024_calendar.png")
    print("Calendar image saved as 2024_calendar.png")



print_month(2024)

 




注:当前文章会不定期进行更新。如果您对本文有更好的建议,有新资料推荐, 可以点击: 欢迎分享优秀网站
这个位置将来会放广告

我想等网站访问量多了,在这个位置放个广告。网站纯公益,但是用爱发电服务器也要钱啊 ----------狂奔的小蜗牛