亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python操作excel之xlwt與xlrd

 更新時(shí)間:2022年12月21日 15:32:36   作者:笑得好美  
這篇文章主要介紹了python使用xlwt與xlrd操作excel,需要的朋友可以參考下

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)鏈接

相關(guān)文章

最新評論