python操作excel之xlwt與xlrd
xlwt與xlrd只能針對xls格式的excel進(jìn)行操作,如果想對xlsx格式進(jìn)行操作請使用openpyxl模板對excel進(jìn)行操作
xlwt寫excel
python安裝xlwt
pip install xlwt
import xlwt
實(shí)例化工作簿對象
book = xlwt.Workbook()
xlwt創(chuàng)建工作表
sheet1 = book.add_sheet("姓名和電話") ???????sheet2 = book.add_sheet("詳情")
xlwt工作表中插入數(shù)據(jù)
sheet1.write(0, 0, "姓名")
xlwt設(shè)置字體樣式
#新建字體 font = xlwt.Font() font.name = "楷體" ???????font.bold = True
創(chuàng)建樣式并設(shè)置
style = xlwt.XFStyle() ???????style.font = font
應(yīng)用樣式
sheet1.write(0, 1, "電話", style)
xlwt批量寫入數(shù)據(jù)
for i in range(10): sheet1.write(i + 1, 0, f"名字{i+1}") ??????? sheet1.write(i + 1, 1, f"電話{i+1}")
xlwt保存工作簿
book.save("學(xué)生信息.xls")
xlrd讀excel
python安裝xlrd
pip install xlrd
import xlrd
xlrd打開創(chuàng)建已有的工作簿對象
book = xlrd.open_workbook("學(xué)生信息.xls")
xlrd獲取當(dāng)前工作簿的工作表名
sheets = book.sheet_names() print(sheets)
xlrd獲取指定的工作表
# (1)索引獲取 sheet1 = book.sheet_by_index(0) print(sheet1) # (2)表名獲取 sheet2 = book.sheet_by_name("詳情") print(sheet2)
xlrd獲取表行數(shù)
rows = sheet1.nrows print(rows)
xlrd獲取表列數(shù)
cols = sheet1.ncols print(cols)
xlrd獲取某行的列寬
row_len = sheet1.row_len(0) print(row_len)
xlrd獲取某行的數(shù)據(jù)(返回列表)
row_values = sheet1.row_values(1) print(row_values)
xlrd獲取某行指定列范圍數(shù)據(jù)(參數(shù)1:行索引;參數(shù)2:起始列索引;參數(shù)3:結(jié)束列索引--不包含在內(nèi))
row_values = sheet1.row_slice(0, 0, 1) print(row_values)
xlrd獲取某列的數(shù)據(jù)(返回列表)
col_values = sheet1.col_values(1) print(col_values)
xlrd獲取某列指定行范圍數(shù)據(jù)(參數(shù)1:列索引;參數(shù)2:起始行索引;參數(shù)3:結(jié)束行索引--不包含在內(nèi))
col_values = sheet1.col_slice(0, 0, 11) print(col_values)
xlrd輸出指定單元格值
cell_value = sheet1.cell(0, 1).value print(cell_value)
本文主要講解了python使用xlwt與xlrd操作excel的知識,更多關(guān)于python操作excel的文章請查看下面的相關(guān)鏈接
- 詳解Python如何實(shí)現(xiàn)對比兩個(gè)Excel數(shù)據(jù)差異
- python辦公自動(dòng)化(Excel)的實(shí)例教程
- 如何使用python讀取Excel指定范圍并轉(zhuǎn)為數(shù)組
- 利用Python讀取Excel表內(nèi)容的詳細(xì)過程
- python合并多個(gè)excel的詳細(xì)過程
- python實(shí)現(xiàn)excel轉(zhuǎn)置問題詳解
- Python讀取excel文件中的數(shù)據(jù),繪制折線圖及散點(diǎn)圖
- python操作excel之openpyxl模塊讀寫xlsx格式使用方法詳解
相關(guān)文章
Python 調(diào)用 ES、Solr、Phoenix的示例代碼
這篇文章主要介紹了Python 調(diào)用 ES、Solr、Phoenix的示例代碼,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-11-11python數(shù)據(jù)類型可變不可變知識點(diǎn)總結(jié)
在本篇文章里小編給各位整理的是關(guān)于python數(shù)據(jù)類型可變不可變知識點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。2020-03-03python函數(shù)調(diào)用,循環(huán),列表復(fù)制實(shí)例
這篇文章主要介紹了python函數(shù)調(diào)用,循環(huán),列表復(fù)制實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python使用pil庫實(shí)現(xiàn)圖片合成實(shí)例代碼
這篇文章主要介紹了python PIL實(shí)現(xiàn)圖片合成實(shí)例代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python hough變換檢測直線的實(shí)現(xiàn)方法
這篇文章主要介紹了python hough變換檢測直線的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07詳解python string類型 bytes類型 bytearray類型
這篇文章主要介紹了python string類型 bytes類型 bytearray類型,需要的朋友可以參考下2017-12-12