Python讀寫Excel表格的方法
更新時間:2021年03月02日 09:57:17 作者:水軍總督
這篇文章主要為大家詳細介紹了Python讀寫Excel表格的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python讀寫Excel表格的具體代碼,供大家參考,具體內容如下
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)) # 獲取某一個位置的數據 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) # 獲取整行和整列的數據 # 第二行數據 row2 = sheet1.row_values(1) print("row2 = {}".format(row2)) # 第二列數據 cols2 = sheet1.col_values(2) print("cols2 = {}".format(cols2)) # python讀取excel中單元格內容為日期的方式 # 返回類型有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 # 寫入數據 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è)務', u'狀態(tài)', u'北京', u'上海', u'廣州', u'深圳', u'狀態(tài)小計', u'合計'] column0 = [u'機票', u'船票', u'火車票', u'汽車票', u'其他'] status = [u'預定', u'出票', u'退票', u'業(yè)務小計'] 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表示跨行個數,n表示跨列個數,string表示要寫入的單元格內容,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'合計', set_style("Time New Roman", 220, True)) i = 0 while i < 4 * len(column0): # 控制外層循環(huán):每次加4 for j in range(0, len(status)): # 控制內層循環(huán):設置每一行內容 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'關系'] 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表格的函數后,會生成data.xls文件。
寫入表格1:
寫入表格2:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python將Office文檔(Word、Excel、PDF、PPT)轉為OFD格式的實現方法
OFD(Open Fixed-layout Document )是我國自主制定的一種開放版式文件格式標準,如果想要通過Python將Office文檔(如Word、Excel或PowerPoint)及PDF文檔轉換為OFD格式,可以參考本文中提供的實現方法,需要的朋友可以參考下2024-06-06