周末師姐讓我?guī)兔μ幚硪幌轮暗尼t(yī)療數(shù)據(jù),數(shù)據(jù)都存放在excel文件中,需要從里面提取出部分?jǐn)?shù)據(jù)并轉(zhuǎn)化為她指定的格式。
總共有20幾個(gè)文件,如果手動(dòng)處理的話,不僅效率低下而且很繁雜,于是我編寫了python腳本來完成了本次的工作,下面記錄一下相關(guān)的知識(shí),并做一個(gè)總結(jié)。
我用到的庫是:openpyxl
首先安裝openpyxl
pip3 install openpyxl #安裝openpyxl
由于本次的工作主要是從excel文件中提取數(shù)據(jù),因此只學(xué)習(xí)了提取數(shù)據(jù)的內(nèi)容,下面的幾段代碼實(shí)現(xiàn)了打開excel文件并從中提取數(shù)據(jù):
import openpyxl
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path) #加載excel文件
sheet_obj = wb_obj.active
print(sheet_obj.max_row) #打印最大的行號(hào)
print(sheet_obj.max_column) #打印最大的列號(hào)
cell_obj = sheet_obj.cell(row = 1, column = 1) #獲取第一行第一列的單元格
print(cell_obj.value) #打印單元格內(nèi)容
此外openpyxl庫還提供了創(chuàng)建excel文件并寫入數(shù)據(jù)的方法,并且還可以對(duì)excel的表格格式進(jìn)行控制。由于本次的任務(wù)沒有涉及到這方面的內(nèi)容,暫時(shí)沒有學(xué)習(xí),等以后需要的時(shí)候再來補(bǔ)充。
參考鏈接:
https://www.geeksforgeeks.org/python-reading-excel-file-using-openpyxl-module/
https://openpyxl.readthedocs.io/en/stable/tutorial.html