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

歡迎訪問 生活随笔!

生活随笔

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

python

用python画值日表

發布時間:2024/3/24 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用python画值日表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目的:學習通過python的PIL庫來做圖片的繪制,幫助熟悉Image,ImageDraw,ImageFont的使用

第一部分,需要用到的函數封裝

1、取星期幾:

days=[ '星期一', '星期二', '星期三', '星期四', '星期五', '星期六','星期日'] def WeekDayName(day=datetime.datetime.weekday(datetime.datetime.now())):return days[day]

2、從文本文件中加載值日人員名單

文件記錄格下如下:

D1=張三|李四|王麻子?

說明,D代表單周(雙周的話,我這里用的是“S"),1表現星期1,等于后面就是人員名單,通過”|“來分割

def LoadContents(DutyFile):SMaxCount = 0DMaxCount = 0if(os.path.exists(DutyFile)):with open(DutyFile, encoding="utf8") as f:for line in f.readlines():line = line.replace('\n','').encode('utf-8').decode('utf-8-sig')if line !='' and line != None and line[0]!='#':tmp = line.split("=")key = tmp[0]value = tmp[1]if key == 'title':self.Caption = valueelse:values = value.split('|')self.Students[key] = valuesif key[0] == 'D':DMaxCount = len(values) if DMaxCount< len(values) else DMaxCountelif key[0] =='S':SMaxCount = len(values) if SMaxCount< len(values) else SMaxCount#print(self.Students)return SMaxCount,DMaxCount

這里返了兩個值,代表單雙周中,人數最多的一例。這里主要是文件的讀寫,沒有什么特別的地方。

3、繪制字符串信息,這里為了美觀,我把所有的文字都居中顯示了

def DrawText(self,xy,text,font,color,draw):x = xy[0] #起點坐標xy = xy[1] #起點坐標Yw = xy[2] #寬h = xy[3] #高text_size = draw.textsize(text,font)x1 = x+(w / 2) - (text_size[0] / 2)y1 = y+(h / 2) - (text_size[1] / 2)draw.ink = color draw.text((x1,y1), text, fill=None, font = font)

4、然后就是畫圖了,具體看代碼

def Draw(self):#1、加載值日人員數據SMaxCount,DMaxCount=self.__LoadContents() #2、計算需要的數據weekNo = self.__CalcWeekNum()iweekday = datetime.datetime.weekday(datetime.datetime.now()) weekdayName = self.__WeekDayName()IfSingleWeek = True if weekNo % 2 == 1 else Falsered = 255 + 0 * 256 + 0 * 256 * 256orange = 255 + 165 * 256 + 0 * 256 * 256maroon =128+0*255+ 0* 256 * 256#3、開始畫圖Width = 770Height = 580bckcolor = (255,255,255)icol = DMaxCount + SMaxCountcols = icol + 2 + 1 #單周行數+又周行數+2標題+1表頭colHeight = int( (Height - 4) / cols)rowWidth = int((Width - 4) / 6)#3.1標題img = Image.new('RGB',size=(Width,Height),color=bckcolor)draw = ImageDraw.Draw(img)# 計算文字居中的位置font = ImageFont.truetype(self.CaptionFont, self.CaptionFontSize)self.__DrawText((2,2,Width,colHeight),self.Caption,font,red,draw)#副標題font = ImageFont.truetype(self.Font, self.FontSize)text = '{0}年{1}月{2}日 {3} 第{4}周'.format(self.CurrentDay.year,self.CurrentDay.month,self.CurrentDay.day, weekdayName,to_chinese(weekNo))self.__DrawText((2,colHeight,rowWidth*2.5,colHeight),text,font,0,draw)#3.2畫表格irow = 5+1wtd = rowWidth * irow + 2;for i in range(0,icol+2):if i <2 or i== DMaxCount+1 or i == icol+1:draw.line((2,colHeight*(i+2) ,wtd, colHeight * (i + 2)),self.BroderColor, width=self.BroderLine)else:draw.line((2 + rowWidth,colHeight*(i+2), wtd, colHeight * (i + 2)),self.BroderColor, width=self.BroderLine)hgt = colHeight * (icol + 3);for i in range(0,irow+2):draw.line((2 + rowWidth * i, colHeight * 2, 2 + rowWidth * i, hgt),self.BroderColor,width=self.BroderLine) draw.line( (2, colHeight * 2, rowWidth, colHeight * 3),self.BroderColor,width=self.BroderLine)#3.3畫表頭GridX = 2;GridY = colHeight * 2 + 2;self.__DrawText((GridX, GridY + colHeight / 2, rowWidth / 2, colHeight / 2),"周數",font,maroon,draw) self.__DrawText((GridX + rowWidth / 2, GridY, rowWidth / 2, colHeight / 2),"星期",font,maroon,draw) font = ImageFont.truetype(self.Font, self.FontSize+6)for i in range(0,5):self.__DrawText((GridX + rowWidth * (i+1), GridY + colHeight * 0, rowWidth, colHeight),days[i],font,maroon,draw) self.__DrawText((GridX, GridY + colHeight * 1, rowWidth, colHeight * DMaxCount),"單周",font,maroon,draw) self.__DrawText((GridX, GridY + colHeight * (DMaxCount+1), rowWidth, colHeight * SMaxCount),"雙周",font,maroon,draw) #3.4畫內容x0 = GridX + rowWidth;y0 = GridY + colHeight;for key,value in self.Students.items():weekflag = key[0]weekday = int(key[1])xoff,yoff = 0,0 fclr = self.FontColorif(weekflag== 'D'): #單周x0+colHeight, y0 + i * colHeight, rowWidth-colHeight, colHeightxoff = rowWidth * (weekday -1)yoff = 0if IfSingleWeek and iweekday+1 == weekday:fclr = redelif(weekflag =='S'):#雙周xoff = rowWidth * (weekday -1)yoff = colHeight* DMaxCount if not IfSingleWeek and iweekday+1 == weekday:fclr = redfont = ImageFont.truetype(self.Font, self.FontSize)for i in range(0,len(value)):if fclr== red:txt = '※ '+value[i]else:txt = value[i]self.__DrawText((x0+xoff,y0+yoff+colHeight*i,rowWidth, colHeight),txt,font,fclr,draw) img.save("d:\zr.bmp", 'bmp', quality = 100) img.show()del draw

這里需要注意,圖片保存的時候,我這里用的BMP格式,因為JPG格式會導致圖片失真,不清楚。

最后,把完整的代碼送給大家。

from PIL import Image,ImageDraw,ImageFont import datetime import osdays=[ '星期一', '星期二', '星期三', '星期四', '星期五', '星期六','星期日'] class DutyTable:titleCount = 2cols = 10rows = 5#標題屬性Caption = '值日表'CaptionFont = r'fonts\SIMLI.TTF'CaptionColor = (0,0,0)CaptionFontSize = 30#字體屬性Font = 'fonts\simsun.ttc'FontSize = 20FontColor = 0#邊框屬性BroderLine = 2BroderColor = "Orange"colHeight = 0rowWidth = 0#第一天,因為學校周數是按學期來計算的,和一年中的第幾周不一樣FirstDay = datetime.date(2018,2,26)#當天CurrentDay = datetime.date.today()DutyFile = r'C:\Users\cdzq-xg\source\DutyTable\DutyTable\bin\Debug\contents.dat'Students = {}#計算第幾周def __CalcWeekNum(self):return int(self.CurrentDay.__sub__(self.FirstDay).days/7)+1def __WeekDayName(self,day=datetime.datetime.weekday(datetime.datetime.now())):return days[day]def __LoadContents(self):SMaxCount = 0DMaxCount = 0if(os.path.exists(self.DutyFile)):with open(self.DutyFile, encoding="utf8") as f:for line in f.readlines():line = line.replace('\n','').encode('utf-8').decode('utf-8-sig')if line !='' and line != None and line[0]!='#':tmp = line.split("=")key = tmp[0]value = tmp[1]if key == 'title':self.Caption = valueelse:values = value.split('|')self.Students[key] = valuesif key[0] == 'D':DMaxCount = len(values) if DMaxCount< len(values) else DMaxCountelif key[0] =='S':SMaxCount = len(values) if SMaxCount< len(values) else SMaxCount#print(self.Students)return SMaxCount,DMaxCountdef __DrawText(self,xy,text,font,color,draw):x = xy[0] #起點坐標xy = xy[1] #起點坐標Yw = xy[2] #寬h = xy[3] #高text_size = draw.textsize(text,font)x1 = x+(w / 2) - (text_size[0] / 2)y1 = y+(h / 2) - (text_size[1] / 2)draw.ink = color draw.text((x1,y1), text, fill=None, font = font)def Draw(self):#1、加載值日人員數據SMaxCount,DMaxCount=self.__LoadContents() #2、計算需要的數據weekNo = self.__CalcWeekNum()iweekday = datetime.datetime.weekday(datetime.datetime.now()) weekdayName = self.__WeekDayName()IfSingleWeek = True if weekNo % 2 == 1 else Falsered = 255 + 0 * 256 + 0 * 256 * 256orange = 255 + 165 * 256 + 0 * 256 * 256maroon =128+0*255+ 0* 256 * 256#3、開始畫圖Width = 770Height = 580bckcolor = (255,255,255)icol = DMaxCount + SMaxCountcols = icol + 2 + 1 #單周行數+又周行數+2標題+1表頭colHeight = int( (Height - 4) / cols)rowWidth = int((Width - 4) / 6)#3.1標題img = Image.new('RGB',size=(Width,Height),color=bckcolor)draw = ImageDraw.Draw(img)# 計算文字居中的位置font = ImageFont.truetype(self.CaptionFont, self.CaptionFontSize)self.__DrawText((2,2,Width,colHeight),self.Caption,font,red,draw)#副標題font = ImageFont.truetype(self.Font, self.FontSize)text = '{0}年{1}月{2}日 {3} 第{4}周'.format(self.CurrentDay.year,self.CurrentDay.month,self.CurrentDay.day, weekdayName,to_chinese(weekNo))self.__DrawText((2,colHeight,rowWidth*2.5,colHeight),text,font,0,draw)#3.2畫表格irow = 5+1wtd = rowWidth * irow + 2;for i in range(0,icol+2):if i <2 or i== DMaxCount+1 or i == icol+1:draw.line((2,colHeight*(i+2) ,wtd, colHeight * (i + 2)),self.BroderColor, width=self.BroderLine)else:draw.line((2 + rowWidth,colHeight*(i+2), wtd, colHeight * (i + 2)),self.BroderColor, width=self.BroderLine)hgt = colHeight * (icol + 3);for i in range(0,irow+2):draw.line((2 + rowWidth * i, colHeight * 2, 2 + rowWidth * i, hgt),self.BroderColor,width=self.BroderLine) draw.line( (2, colHeight * 2, rowWidth, colHeight * 3),self.BroderColor,width=self.BroderLine)#3.3畫表頭GridX = 2;GridY = colHeight * 2 + 2;self.__DrawText((GridX, GridY + colHeight / 2, rowWidth / 2, colHeight / 2),"周數",font,maroon,draw) self.__DrawText((GridX + rowWidth / 2, GridY, rowWidth / 2, colHeight / 2),"星期",font,maroon,draw) font = ImageFont.truetype(self.Font, self.FontSize+6)for i in range(0,5):self.__DrawText((GridX + rowWidth * (i+1), GridY + colHeight * 0, rowWidth, colHeight),days[i],font,maroon,draw) self.__DrawText((GridX, GridY + colHeight * 1, rowWidth, colHeight * DMaxCount),"單周",font,maroon,draw) self.__DrawText((GridX, GridY + colHeight * (DMaxCount+1), rowWidth, colHeight * SMaxCount),"雙周",font,maroon,draw) #3.4畫內容x0 = GridX + rowWidth;y0 = GridY + colHeight;for key,value in self.Students.items():weekflag = key[0]weekday = int(key[1])xoff,yoff = 0,0 fclr = self.FontColorif(weekflag== 'D'): #單周x0+colHeight, y0 + i * colHeight, rowWidth-colHeight, colHeightxoff = rowWidth * (weekday -1)yoff = 0if IfSingleWeek and iweekday+1 == weekday:fclr = redelif(weekflag =='S'):#雙周xoff = rowWidth * (weekday -1)yoff = colHeight* DMaxCount if not IfSingleWeek and iweekday+1 == weekday:fclr = redfont = ImageFont.truetype(self.Font, self.FontSize)for i in range(0,len(value)):if fclr== red:txt = '※ '+value[i]else:txt = value[i]self.__DrawText((x0+xoff,y0+yoff+colHeight*i,rowWidth, colHeight),txt,font,fclr,draw) img.save("d:\zr.bmp", 'bmp', quality = 100) img.show()del drawif __name__ == '__main__':DutyTable().Draw()

總結

以上是生活随笔為你收集整理的用python画值日表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。