python 处理 excel格式文件
生活随笔
收集整理的這篇文章主要介紹了
python 处理 excel格式文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python 調(diào)用xlrd package就可以處理excel文件. 簡單例子如下:
源碼如下:
# -*- coding: utf-8 -*-
import xlrd?
#----------------------------------------------------------------------
def open_file(path):
? ? """
? ? Open and read an Excel file
? ? """
? ? book = xlrd.open_workbook(path)
?
? ? ncols = 0
? ? nrows = 0
? ? for n, s in enumerate(book.sheets()): ? ? ? ? ??
? ? ? ? #print "Sheet %d is called %s and has %d columns and %d rows" % (n, s.name, s.ncols, s.nrows)
? ? ? ? nrows = s.nrows
? ? ? ? ncols = s.ncols
? ? print "rows length is: " + str(nrows) + "\n"
? ? ?
? ? print "columns length is: " + str(ncols) + "\n"
? ??
? ? part_name2 = ""
? ? part_name1 = ""
? ? file_flag = 0
? ? first_sheet = book.sheet_by_index(0)
? ? for n in xrange(0, nrows):
? ? ? ?
? ? ? ? cells = first_sheet.row_slice(rowx=n,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? start_colx=0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? end_colx=4)
? ? ? ??
? ? ? ? for cell in cells:
? ? ? ? ? print cell.value
#----------------------------------------------------------------------
if __name__ == "__main__":
? ? path = "file_score.xls"
? ? open_file(path)
總結(jié)
以上是生活随笔為你收集整理的python 处理 excel格式文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot2稳定版本_重要版本
- 下一篇: SpringBoot中注入Applica