Python讀寫Excel表格的方法
本文實(shí)例為大家分享了Python讀寫Excel表格的具體代碼,供大家參考,具體內(nèi)容如下
python讀取Excel表格:
import xlrd
def read_excel():
# 打開文件
wb = xlrd.open_workbook(r'test.xls')
# 獲取所有sheet的名字
print(wb.sheet_names())
# 獲取第二個sheet的表名
sheet2 = wb.sheet_names()[1]
print("sheet2 = {}".format(sheet2))
# sheet1索引從0開始,得到sheet1表的句柄
sheet1 = wb.sheet_by_index(0)
rowNum = sheet1.nrows
colNum = sheet1.ncols
print("rowNum = {}, colNum = {}".format(rowNum, colNum))
# 獲取某一個位置的數(shù)據(jù)
c1_0 = sheet1.cell(1, 0).value
print("c1_0 = {}".format(c1_0))
# 1 ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
print(sheet1.cell(1, 2).ctype)
# 獲取整行和整列的數(shù)據(jù)
# 第二行數(shù)據(jù)
row2 = sheet1.row_values(1)
print("row2 = {}".format(row2))
# 第二列數(shù)據(jù)
cols2 = sheet1.col_values(2)
print("cols2 = {}".format(cols2))
# python讀取excel中單元格內(nèi)容為日期的方式
# 返回類型有5種
print("for循環(huán):")
for i in range(rowNum):
# if sheet1.cell(i, 2).ctype == 1:
# d = xlrd.xldate_as_tuple(sheet1.cell_value(i, 2), wb.datemode)
# print(date(*d[:3]), end='')
print(sheet1.cell(i, 2))
# 輸出如下:
# ['我的第一個表', '第二個', '呵呵第三個']
# sheet2 = 第二個
# rowNum = 8, colNum = 3
# c1_0 = w
# 2
# row2 = ['w', 's', 10.0]
# cols2 = ['z', 10.0, 666.0, '2021年2月25日 02:06:25', 44252.0, 'x', 1, '']
# for循環(huán):
# text:'z'
# number:10.0
# number:666.0
# text:'2021年2月25日 02:06:25'
# xldate:44252.0
# text:'x'
# bool:1
# empty:''

python寫入Excel表格:
import xlwt
# 寫入數(shù)據(jù)
def write_excel():
f = xlwt.Workbook()
# 創(chuàng)建表sheet1
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
# 如果是寫入中文,則要用u'漢字'的形式。比如 sheet1.write(0,0, u'漢字')
row0 = [u'業(yè)務(wù)', u'狀態(tài)', u'北京', u'上海', u'廣州', u'深圳', u'狀態(tài)小計(jì)', u'合計(jì)']
column0 = [u'機(jī)票', u'船票', u'火車票', u'汽車票', u'其他']
status = [u'預(yù)定', u'出票', u'退票', u'業(yè)務(wù)小計(jì)']
for i in range(0, len(row0)):
sheet1.write(0, i, row0[i], set_style("Time New Roman", 220, True))
# 合并單元格:
# sheet1.write_merge(x, x + m, y, y + n, string, style)
# x表示行,y表示列,m表示跨行個數(shù),n表示跨列個數(shù),string表示要寫入的單元格內(nèi)容,style表示單元格樣式。
i, j = 1, 0
while i < 4 * len(column0): # 控制循環(huán):每次加4
# 第一列
sheet1.write_merge(i, i + 3, 0, 0, column0[j], set_style('Arial', 220, True))
# 最后一列
sheet1.write_merge(i, i + 3, 7, 7)
i += 4
j += 1
sheet1.write_merge(21, 21, 0, 1, u'合計(jì)', set_style("Time New Roman", 220, True))
i = 0
while i < 4 * len(column0): # 控制外層循環(huán):每次加4
for j in range(0, len(status)): # 控制內(nèi)層循環(huán):設(shè)置每一行內(nèi)容
sheet1.write(i + j + 1, 1, status[j])
i += 4
# 創(chuàng)建sheet2
sheet2 = f.add_sheet(u'sheet2',cell_overwrite_ok=True)
row0 = [u'姓名', u'年齡', u'出生日期', u'愛好', u'關(guān)系']
column0 = [u'UZI', u'Faker', u'大司馬', u'PDD', u'馮提莫']
# 生成第一行
for i in range(0, len(row0)):
sheet2.write(0, i, row0[i], set_style('Times New Roman', 220, True))
# 生成第一列
for i in range(0, len(column0)):
sheet2.write(i + 1, 0, column0[i], set_style('Times New Roman', 220, True))
f.save('data.xls')
執(zhí)行上面這個寫入excel表格的函數(shù)后,會生成data.xls文件。
寫入表格1:

寫入表格2:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python Sweetviz輕松實(shí)現(xiàn)探索性數(shù)據(jù)分析
Sweetviz是一個開放源代碼Python庫,可生成精美的高密度可視化文件,以單行代碼啟動EDA(探索性數(shù)據(jù)分析)。輸出是一個完全獨(dú)立的HTML應(yīng)用程序,該系統(tǒng)圍繞快速可視化目標(biāo)值和比較數(shù)據(jù)集而構(gòu)建。其目標(biāo)是幫助快速分析目標(biāo)特征,訓(xùn)練與測試數(shù)據(jù)以及其他此類數(shù)據(jù)表征任務(wù)2021-11-11
Python循環(huán)取數(shù)組的值的方法實(shí)現(xiàn)
本文主要介紹了兩種Python中遍歷數(shù)組的方法,for循環(huán)和索引,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Python二進(jìn)制數(shù)據(jù)結(jié)構(gòu)Struct的具體使用
在C/C++語言中,struct被稱為結(jié)構(gòu)體。而在Python中,struct是一個專門的庫,用于處理字節(jié)串與原生Python數(shù)據(jù)結(jié)構(gòu)類型之間的轉(zhuǎn)換。本文就詳細(xì)介紹struct的使用方式2021-06-06
python微信公眾號開發(fā)簡單流程實(shí)現(xiàn)
這篇文章主要介紹了python微信公眾號開發(fā)簡單流程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

