日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python对Excel的操作 xlrd、xlwt包详解

發布時間:2024/7/23 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python对Excel的操作 xlrd、xlwt包详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章更新中.............

python中對Excel表格的操作,主要用到兩個包,分別是xlrd和xlwt

1.xlrd主要用于讀Excel表

2.xlwt則主要用于寫Excel

  • python—xlrd

這里我先準備了一些濟南天氣的一些數據


通過python程序先對數據有一個基本的了解:

path = 'F:\\文件存放處\\weather\\濟南.xls'import xlrd import xlwt from datetime import date, datetime'''先定義一個函數,對我們所擁有的數據做一個基本的了解''' def read_excel():# 打開文件workbook = xlrd.open_workbook(path)# 獲取所有的sheetprint(workbook.sheet_names()) # ['sheet1', 'sheet2']sheet1_name = workbook.sheet_names()[0]#sheet1 = workbook.sheet_by_index(0)print(sheet1)sheet1 = workbook.sheet_by_name('濟南2018年4月份天氣詳情')print(sheet1)# 顯示sheet的名稱、行數、列數print(sheet1.name, sheet1.nrows, sheet1.ncols)# 獲取整行和整列的值rows = sheet1.row_values(5) # 第六行的數據cols = sheet1.col_values(2) # 第三列的數據print(rows)print(cols)# 獲取單元格內容(四種獲取方式)print(sheet1.cell(1, 0).value.encode('utf-8'))print(sheet1.cell_value(1, 0).encode('utf-8'))print(sheet1.row_values(1)[0].encode('utf-8'))print(sheet1.col_values(0)[1].encode('utf-8'))# 獲取單元格內容的數據類型print(sheet1.cell(1, 0).ctype)if __name__ == '__main__':read_excel()

運行結果如下:

['濟南2018年4月份天氣詳情', '濟南2018年3月份天氣詳情', '濟南2018年2月份天氣詳情', '濟南2018年1月份天氣詳情', '濟南2017年12月份天氣詳情', '濟南2017年11月份天氣詳情', '濟南2017年10月份天氣詳情', '濟南2017年9月份天氣詳情', '濟南2017年8月份天氣詳情', '濟南2017年7月份天氣詳情', '濟南2017年6月份天氣詳情', '濟南2017年5月份天氣詳情', '濟南2017年4月份天氣詳情', '濟南2017年3月份天氣詳情', '濟南2017年2月份天氣詳情', '濟南2017年1月份天氣詳情', '濟南2016年12月份天氣詳情', '濟南2016年11月份天氣詳情', '濟南2016年10月份天氣詳情', '濟南2016年9月份天氣詳情', '濟南2016年8月份天氣詳情', '濟南2016年7月份天氣詳情', '濟南2016年6月份天氣詳情', '濟南2016年5月份天氣詳情', '濟南2016年4月份天氣詳情', '濟南2016年3月份天氣詳情', '濟南2016年2月份天氣詳情', '濟南2016年1月份天氣詳情', '濟南2015年12月份天氣詳情', '濟南2015年11月份天氣詳情', '濟南2015年10月份天氣詳情', '濟南2015年9月份天氣詳情', '濟南2015年8月份天氣詳情', '濟南2015年7月份天氣詳情', '濟南2015年6月份天氣詳情', '濟南2015年5月份天氣詳情', '濟南2015年4月份天氣詳情', '濟南2015年3月份天氣詳情', '濟南2015年2月份天氣詳情', '濟南2015年1月份天氣詳情', '濟南2014年12月份天氣詳情', '濟南2014年11月份天氣詳情', '濟南2014年10月份天氣詳情', '濟南2014年9月份天氣詳情', '濟南2014年8月份天氣詳情', '濟南2014年7月份天氣詳情', '濟南2014年6月份天氣詳情', '濟南2014年5月份天氣詳情', '濟南2014年4月份天氣詳情', '濟南2014年3月份天氣詳情', '濟南2014年2月份天氣詳情', '濟南2014年1月份天氣詳情', '濟南2013年12月份天氣詳情', '濟南2013年11月份天氣詳情', '濟南2013年10月份天氣詳情', '濟南2013年9月份天氣詳情', '濟南2013年8月份天氣詳情', '濟南2013年7月份天氣詳情', '濟南2013年6月份天氣詳情', '濟南2013年5月份天氣詳情', '濟南2013年4月份天氣詳情', '濟南2013年3月份天氣詳情', '濟南2013年2月份天氣詳情', '濟南2013年1月份天氣詳情', '濟南2012年12月份天氣詳情', '濟南2012年11月份天氣詳情', '濟南2012年10月份天氣詳情', '濟南2012年9月份天氣詳情', '濟南2012年8月份天氣詳情', '濟南2012年7月份天氣詳情', '濟南2012年6月份天氣詳情', '濟南2012年5月份天氣詳情', '濟南2012年4月份天氣詳情', '濟南2012年3月份天氣詳情', '濟南2012年2月份天氣詳情', '濟南2012年1月份天氣詳情', '濟南2011年12月份天氣詳情', '濟南2011年11月份天氣詳情', '濟南2011年10月份天氣詳情', '濟南2011年9月份天氣詳情', '濟南2011年8月份天氣詳情', '濟南2011年7月份天氣詳情', '濟南2011年6月份天氣詳情', '濟南2011年5月份天氣詳情', '濟南2011年4月份天氣詳情', '濟南2011年3月份天氣詳情', '濟南2011年2月份天氣詳情', '濟南2011年1月份天氣詳情'] <xlrd.sheet.Sheet object at 0x000001F2F2631C50> <xlrd.sheet.Sheet object at 0x000001F2F2631C50> 濟南2018年4月份天氣詳情 31 6 ['2018-04-05', '8', '1', '陰', '西北風', '微風'] ['最低氣溫', '20', '15', '6', '4', '1', '1', '5', '13', '17', '14', '16', '11', '6', '7', '11', '15', '18', '20', '22', '23', '11', '10', '9', '8', '16', '15', '17', '22', '21', '19'] b'2018-04-01' b'2018-04-01' b'2018-04-01' b'2018-04-01' 1Process finished with exit code 0

(1)python讀取Excel中單元格的內容返回的有5種類型,即上面示例中的ctype

ctype: 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error

那么,我們先看一下我們所擁有的數據類型

# 獲取單元格內容的數據類型print(sheet1.cell(1, 0).value, sheet1.cell(1, 0).ctype)print(sheet1.cell(1, 1).value, sheet1.cell(1, 1).ctype)print(sheet1.cell(1, 2).value, sheet1.cell(1, 2).ctype)print(sheet1.cell(1, 3).value, sheet1.cell(1, 3).ctype)print(sheet1.cell(1, 4).value, sheet1.cell(1, 4).ctype)print(sheet1.cell(1, 5).value, sheet1.cell(1, 5).ctype) 運行結果為: 2018-04-01 1 30 1 20 1 晴 1 東南風 1 2級 1

這么說我們所擁有的數據全部為string類型。就連日期都沒有出現date類型,內心不禁感到莞爾一笑。

當單元格的ctype = 3 時,說明該單元格的數據為date類型,這時需要用xlrd的xldate_as_tuple來處理為date格式,但是,需要先判斷單元格的ctype=3時,才能用此函數進行操作。

date_value = xlrd.xldate_as_tuple(sheet1.cell_value(1, 0), workbook.datemode) if (sheet1.cell(row, col).ctype == 3):date_value = xlrd.xldate_as_tuple(sheet1.cell_value(row, col), workbook.datemode)

也可以將date類型在轉化為str類型

date_value = (2018, 4, 1, 0, 0) date_change = date(*date_value[:3]).strftime('%Y/%m/%d') print(date_change) print(type(date_change))

程序運行結果如下:

2018/04/01 <class 'str'>由上可知ctype的值不同,則類型也不同,處理情況與date類型類似,這里就不一一解釋了。

(2)獲取合并的單元格


總結

以上是生活随笔為你收集整理的python对Excel的操作 xlrd、xlwt包详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。