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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python操作excel之 模块 xlrd

發(fā)布時(shí)間:2025/5/22 python 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python操作excel之 模块 xlrd 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

xlrd是專門(mén)用來(lái)在python中讀取微軟execel的模塊,可以自己直接下載安裝,也可以通過(guò)包管理器安裝。

?

官方資料:

下載地址:http://pypi.python.org/pypi/xlrd

官網(wǎng)地址:http://www.python-excel.org/

文檔地址:https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html

文檔pdf下載:http://www.simplistix.co.uk/presentations/python-excel.pdf

?

基本操作:

[python]?view plaincopy
  • #?encoding?:?utf-8???????#設(shè)置編碼方式??
  • ??
  • import?xlrd????????????????????#導(dǎo)入xlrd模塊??
  • ??
  • #打開(kāi)指定文件路徑的excel文件??
  • ??
  • xlsfile?=?r'D:\AutoPlan\apisnew.xls'???
  • book?=?xlrd.open_workbook(xlsfile)?????#獲得excel的book對(duì)象??
  • ??
  • #獲取sheet對(duì)象,方法有2種:??
  • sheet_name=book.sheet_names()[0]??????????#獲得指定索引的sheet名字??
  • print?sheet_name??
  • sheet1=book.sheet_by_name(sheet_name)??#通過(guò)sheet名字來(lái)獲取,當(dāng)然如果你知道sheet名字了可以直接指定??
  • sheet0=book.sheet_by_index(0)?????#通過(guò)sheet索引獲得sheet對(duì)象??
  • ??
  • #獲取行數(shù)和列數(shù):??
  • ??
  • nrows?=?sheet.nrows????#行總數(shù)??
  • ncols?=?sheet.ncols???#列總數(shù)??
  • ??
  • #獲得指定行、列的值,返回對(duì)象為一個(gè)值列表??
  • ??
  • row_data?=?sheet.row_values(0)???#獲得第1行的數(shù)據(jù)列表??
  • col_data?=?sheet.col_values(0)??#獲得第一列的數(shù)據(jù)列表,然后就可以迭代里面的數(shù)據(jù)了??
  • ??
  • #通過(guò)cell的位置坐標(biāo)獲得指定cell的值??
  • cell_value1?=?sheet.cell_value(0,1)??##只有cell的值內(nèi)容,如:http://xxx.xxx.xxx.xxx:8850/2/photos/square/??
  • print?cell_value1??
  • cell_value2?=?sheet.cell(0,1)?##除了cell值內(nèi)容外還有附加屬性,如:text:u'http://xxx.xxx.xxx.xxx:8850/2/photos/square/'??
  • print?cell_value2??

  • 是不是很方便啊,恩,比用vbs調(diào)用的excel COM對(duì)象簡(jiǎn)便多了。而且這個(gè)支持linux平臺(tái)。

    ?

    =====================================xls的寫(xiě)方法使用xlwt模塊===================================================

    ?

    [python]?view plaincopy
  • #encoding:utf-8???????#設(shè)置編碼方式????
  • ????
  • import?xlwt??
  • wbk?=?xlwt.Workbook(encoding='utf-8',?style_compression=0)??
  • sheet?=?wbk.add_sheet('sheet?1',?cell_overwrite_ok=True)??##第二參數(shù)用于確認(rèn)同一個(gè)cell單元是否可以重設(shè)值。??
  • ??
  • sheet.write(0,0,'some?text')??
  • sheet.write(0,0,'this?should?overwrite')???##重新設(shè)置,需要cell_overwrite_ok=True??
  • ??
  • style?=?xlwt.XFStyle()??
  • font?=?xlwt.Font()??
  • font.name?=?'Times?New?Roman'??
  • font.bold?=?True??
  • style.font?=?font??
  • sheet.write(0,?1,?'some?bold?Times?text',?style)??
  • ??
  • wbk.save('D:\TestData2.xls')????##該文件名必須存在??
  • 總結(jié)

    以上是生活随笔為你收集整理的python操作excel之 模块 xlrd的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。