欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python讀寫Excel表格的方法

 更新時(shí)間:2021年03月02日 09:57:17   作者:水軍總督  
這篇文章主要為大家詳細(xì)介紹了Python讀寫Excel表格的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python讀寫Excel表格的具體代碼,供大家參考,具體內(nèi)容如下

python讀取Excel表格:

import xlrd 
 
def read_excel():
 # 打開文件
 wb = xlrd.open_workbook(r'test.xls')
 # 獲取所有sheet的名字
 print(wb.sheet_names())
 # 獲取第二個(gè)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))
 # 獲取某一個(gè)位置的數(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))
 
# 輸出如下:
# ['我的第一個(gè)表', '第二個(gè)', '呵呵第三個(gè)']
# sheet2 = 第二個(gè)
# 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表示跨行個(gè)數(shù),n表示跨列個(gè)數(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í)行上面這個(gè)寫入excel表格的函數(shù)后,會(huì)生成data.xls文件。

寫入表格1:

寫入表格2:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論