python办公自动化练习——体温
目錄
一、題目描述
二、效果展示
?三、源碼展示
四、分析
1、獲取工作簿對象
2、創(chuàng)建工作表對象
3、讀取excel表中的數(shù)據(jù)
4、將表頭寫入工作表
5、將數(shù)據(jù)寫入工作表
6、體溫樣式
7、狀態(tài)標(biāo)識
一、題目描述
對體溫測量登記表中的數(shù)據(jù)篩選處理,將體溫不正常的數(shù)據(jù)用顏色填充,以及顯示體溫的狀態(tài)。
二、效果展示
?三、源碼展示
import xlwt import xlrd# 根據(jù)體溫設(shè)置背景顏色def get_temp_status(temp):if temp < 37.2:status = '正常'elif temp < 38.5:status = '發(fā)熱'else:status = '高熱'return statusdef get_temp_style(temp):style = xlwt.XFStyle()pattern = xlwt.Pattern()pattern.pattern = xlwt.Pattern.SOLID_PATTERNif temp < 37.2:pattern.pattern_fore_colour = xlwt.Style.colour_map['green']elif temp < 38.5:pattern.pattern_fore_colour = xlwt.Style.colour_map['orange']else:pattern.pattern_fore_colour = xlwt.Style.colour_map['red']style.pattern = patternreturn styledef main():wb = xlrd.open_workbook('體溫測量登記表.xls')#獲取工作簿對象sheet1 = wb.sheet_by_index(0)#通過下標(biāo)索引創(chuàng)建一個工作表對象data = []for row in range(1, sheet1.nrows):record = []for col in range(sheet1.ncols):record.append(sheet1.cell(row, col).value)data.append(record)wb2 = xlwt.Workbook()#創(chuàng)建一個寫的工作簿對象sheet2 = wb2.add_sheet('帶顏色標(biāo)記的體溫測量登記表')sheet2.write(0, 0, '姓名')sheet2.write(0, 1, '體溫')sheet2.write(0, 2, '狀態(tài)')count = 0for index, record in enumerate(data):num, temp = recordif temp >= 37.2:count += 1sheet2.write(index + 1, 0, num)sheet2.write(index + 1, 1, temp, get_temp_style(temp))sheet2.write(index + 1, 2, get_temp_status(temp))sheet2.write(102, 0, '總?cè)藬?shù)')sheet2.write(102, 1, f'{len(data)}人')sheet2.write(103, 0, '異常人數(shù)')sheet2.write(103, 1, f'{count}人')wb2.save('體溫測量登記表_最全.xls')if __name__ == "__main__":main()四、分析
1、獲取工作簿對象
wb = xlrd.open_workbook('體溫測量登記表.xls')#獲取工作簿對象2、創(chuàng)建工作表對象
sheet1 = wb.sheet_by_index(0)#通過下標(biāo)索引創(chuàng)建一個工作表對象3、讀取excel表中的數(shù)據(jù)
????????可以將excel表中的數(shù)據(jù)看做是二維的數(shù)組(python中稱為列表),先讀取第一行的每一列,然后依次的讀取每一行,也就是用到兩個for循環(huán)去實現(xiàn),python對應(yīng)的就是列表了,將一行的數(shù)據(jù)存儲到一維的列表中,然后再將列表存儲到列表中,就形成了二維的列表。
data = []for row in range(1, sheet1.nrows):record = []for col in range(sheet1.ncols):record.append(sheet1.cell(row, col).value)data.append(record)4、將表頭寫入工作表
sheet2 = wb2.add_sheet('帶顏色標(biāo)記的體溫測量登記表')sheet2.write(0, 0, '姓名')#第0行的第0列寫入姓名sheet2.write(0, 1, '體溫')#第0行第一列寫入體溫sheet2.write(0, 2, '狀態(tài)')#第0行第二列寫入狀態(tài)#此三處寫入就相當(dāng)于是寫表頭了5、將數(shù)據(jù)寫入工作表
? ? ? ? 之前從excel表中讀取數(shù)據(jù),現(xiàn)在寫到另一個表中,因為不能在源文件上做更改(原數(shù)據(jù)要保留);
? ? ? ? 存入的是一個二維列表,取出時用枚舉的方法,這樣不僅能去到二維列表里面一維列表還能得到它是位于第幾位的列表,此時的位置加1就是寫入文件的行數(shù),列數(shù)就是0,1,2,內(nèi)容就是姓名、體溫、以及狀態(tài),但是此處的體溫的樣式是做了改變的
count = 0for index, record in enumerate(data):num, temp = record#解包if temp >= 37.2:count += 1sheet2.write(index + 1, 0, num)sheet2.write(index + 1, 1, temp, get_temp_style(temp))sheet2.write(index + 1, 2, get_temp_status(temp))6、體溫樣式
????????在寫Excel文件時,我們還可以為單元格設(shè)置樣式,主要包括字體(Font)、對齊方式(Alignment)、邊框(Border)和背景(Background)的設(shè)置,xlwt對這幾項設(shè)置都封裝了對應(yīng)的類來支持。
????????要設(shè)置單元格樣式需要首先創(chuàng)建一個XFStyle對象,再通過該對象的屬性對字體、對齊方式、邊框等進(jìn)行設(shè)定
? ? ? ? 對于樣式的設(shè)置不用了解的很深刻,需要用到的時候套用即可
def get_temp_style(temp):style = xlwt.XFStyle()pattern = xlwt.Pattern()pattern.pattern = xlwt.Pattern.SOLID_PATTERN#填充if temp < 37.2:pattern.pattern_fore_colour = xlwt.Style.colour_map['green'] #當(dāng)溫度小于37.2度時用綠色填充elif temp < 38.5:pattern.pattern_fore_colour = xlwt.Style.colour_map['orange'] #溫度小于38.5度大于37.2度時用橙色填充else: #小于37.2或者大于38。5的溫度用紅色填充pattern.pattern_fore_colour = xlwt.Style.colour_map['red']style.pattern = patternreturn style#返回樣式對象7、狀態(tài)標(biāo)識
? ? ? ? 這個就相對簡單了,就只用根據(jù)溫度的大小去返回對應(yīng)的值即可
def get_temp_status(temp):if temp < 37.2:status = '正常'elif temp < 38.5:status = '發(fā)熱'else:status = '高熱'return status總結(jié)
以上是生活随笔為你收集整理的python办公自动化练习——体温的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 郑州学车驾校考驾照流程攻略
- 下一篇: stata软件不出图_绘制回归分析结果的