python讀寫Excel方法(xlwt和xlrd)

  在咱們作日常工做中都會遇到操做excel,那麼今天寫一篇,如何經過python操做excel,固然python操做excel的庫有不少,好比pandas,xlwt/xlrd,openpyxl等,每一個庫都有不一樣的區別,具體的區別,你們一塊兒研究下哈。python

 xlrd模塊

xlrd是對於Excel進行讀取,xlrd 操做的是xls/xlxs格式的excel函數

安裝

xlrd是python的第3方庫,須要經過pip進行安裝學習

pip install xlrd

 讀取excel數據

一、導入xlrd莫款測試

二、打開Excel完成實例化字體

三、經過下標獲取對應的表(能夠經過表名獲取)spa

四、經過列,行或者座標獲取表格的數據excel

# coding:utf-8
import xlrd
# excel路徑
excle_path = r'E:\123.xlsx'
# 打開excel讀取文件
data = xlrd.open_workbook(excle_path)
# 根據sheet下標選擇讀取內容
sheet = data.sheet_by_index(1)
# 獲取到表的總行數
nrows = sheet.nrows
for i in range(nrows):
    print(sheet.row_values(i))

 上面的是經過表的下標來選擇讀取的,當咱們知道表的名稱的時候,也能夠經過表的名稱進行讀取code

# coding:utf-8
import xlrd
# excel路徑
excle_path = r'E:\123.xlsx'
# 打開excel讀取文件
data = xlrd.open_workbook(excle_path)
# 根據sheet下標選擇讀取內容
sheet = data.sheet_by_index(1)
# 獲取所有表的名稱
sheet_names = data.sheet_names()
print('所有表名稱')
print(sheet_names)
# 根據sheet名稱選擇讀取內容
sheet1 = data.sheet_by_name('姓名')
# 獲取到表的總行數
nrows1 = sheet.nrows
print('姓名錶內容:')
for j in range(nrows1):
    print(sheet1.row_values(j))

上面經過下標和名稱都成功讀取出來了數據,咱們經過座標來讀取數據orm

# coding:utf-8
import xlrd
# excel路徑
excle_path = r'E:\123.xlsx'
# 打開excel讀取文件
data = xlrd.open_workbook(excle_path)
# 根據sheet下標選擇讀取內容
sheet = data.sheet_by_index(1)
# 經過座標讀取
A1 = sheet.cell_value(1,0)
print(A1)

 

xlrd中的其餘用法

一、讀取sheet的行數、列數blog

# excel路徑
excle_path = r'E:\123.xlsx'
# 打開excel讀取文件
data = xlrd.open_workbook(excle_path)
# 讀取sheet行數據,經過下標獲取
a = sheet.row_values(1)
# 讀取sheet列數據,經過下標獲取
b = sheet.col_values(1)

二、獲取sheet的行數,列數

# coding:utf-8
import xlrd
# excel路徑
excle_path = r'E:\123.xlsx'
# 打開excel讀取文件
data = xlrd.open_workbook(excle_path)
# 根據sheet下標選擇讀取內容
sheet = data.sheet_by_index(1)
print('sheet名稱:{}\nsheet列數: {}\nsheet行數: {}'.format(sheet.name, sheet.ncols, sheet.nrows))

 xlwt模塊

xlwt是對於Excel進行讀取,xlwt操做的是xls格式的excel

安裝

xlwt屬於python的第3方庫,須要經過pip進行安裝

pip install xlwt

 寫入Excel數據

一、首先導入xlwt第3方庫

二、建立一個workbook模塊,至關於建立一個xlwt文件

三、經過add_sheet建立一個表格

四、使用write函數進行對錶完成寫的操做

五、把寫完的數據導入到Excel中

# coding:utf-8
import xlwt
# excel路徑
excle_path = r'E:\1234.xls'
# 建立一個Workbook模塊
data = xlwt.Workbook(encoding='utf-8')
# 建立一個表格,cell_overwrite_ok=True 爲不覆蓋表,默認爲False
sheet = data.add_sheet('test123',cell_overwrite_ok=True)
# 寫入座標爲(0,0)內容爲職位
sheet.write(0,0,'職位')
# 寫入座標爲(1,0)內容爲軟件測試工程師
sheet.write(1,0,'軟件測試工程師')
# 保存到excel中
data.save(excle_path)

找到excel的路徑中打開查看,發現已經寫入成功了

 

Xlwt的其餘方法

 其中xlwt這些操做Excel只是默認的方法,xlwt也能夠更改寫入內容的文字大小,顏色等操做

style = xlwt.XFStyle() # 初始化樣式
font = xlwt.Font()  # 建立字體
font.name = u'微軟雅黑'  # 字體類型
font.colour_index = 6    #字體顏色
font.underline = True  #下劃線
font.italic = True  # 斜體
font.height = 400    #字體大小   200等於excel字體大小中的10
style.font = font    #設定樣式

若是哪些內容寫入時須要更改上面的格式內容,能夠在書寫的後面加上方法

給你們舉個小栗子

# coding:utf-8
import xlwt
# excel路徑
excle_path = r'E:\12314.xls'
# 建立一個Workbook模塊
data = xlwt.Workbook(encoding='utf-8')
style = xlwt.XFStyle() # 初始化樣式
font = xlwt.Font()  # 建立字體
font.name = u'微軟雅黑' # 字體類型
font.colour_index = 6   # 字體顏色
font.underline = True # 下劃線
font.italic = True # 斜體
font.height = 400    # 字體大小   200等於excel字體大小中的10
style.font = font   # 設定樣式
# 建立一個表格,cell_overwrite_ok=True 爲不覆蓋表,默認爲False
sheet = data.add_sheet('test123',cell_overwrite_ok=True)
# 寫入座標爲(0,0)內容爲職位
sheet.write(0,0,'職位',style)
# 寫入座標爲(1,0)內容爲軟件測試工程師
sheet.write(1,0,'軟件測試工程師',style)
# 保存到excel中
data.save(excle_path)

 

 

 

固然python操做Excel的方法不單單是這麼多,還有更多的騷操做,等到咱們用到的時候在一塊兒學習哈

 

感受安靜寫的對您有幫助的話,能夠點個關注,持續更新中~~

相關文章
相關標籤/搜索