安裝
- 讀Excel文件通過模塊xlrd
- 寫Excel文件同過模塊xlwt(可惜的是只支持Python2.3到Python2.7版本)
- xlwt-future模塊,支持Python3.X,用法據說與xlwt模塊一模一樣
- Excel2007往后版本多了一個xlsx文件類型,是為了使Excel能存入超過65535行數據(1048576),所以讀寫xlsx文件需要另一個庫叫openpyxl,支持Python3.x
pip install xlrd
,還能更簡單點嗎?
使用參考:xlrd官網
安裝的版本為0.9.3,但是官網的介紹還是關于Version 0.7.3版本的,無妨,不影響理解。
Tutorial PDF指向的API url也404了,不怕,我們還有help()。
讀取Excel:
from mmap import mmap, ACCESS_READ from xlrd import open_workbook testxls = './剩余工作LIST.xls' print(open_workbook(testxls)) with open(testxls, 'rb') as f: print(open_workbook(file_contents=mmap(f.fileno(),0,access=ACCESS_READ))) wb = open_workbook(testxls) for s in wb.sheets(): print ('Sheet:',s.name) for row in range(s.nrows): values = [] for col in range(s.ncols): values.append(s.cell(row,col).value) print (','.join(str(values)))
Getting a particular Cell(獲取特定的Cell)
from xlrd import open_workbook,XL_CELL_TEXT book = open_workbook(testxls) sheet = book.sheet_by_index(0) # cell = sheet.cell(0,0) # print(cell) # print(cell.value) # print(cell.ctype==XL_CELL_TEXT) for i in range(sheet.ncols): print (sheet.cell_type(1,i),sheet.cell_value(1,i))
Iterating over the contents of a Sheet(迭代Sheet中的內容)
from xlrd import open_workbook book = open_workbook(testxls) sheet0 = book.sheet_by_index(0) sheet1 = book.sheet_by_index(1) print(sheet0.row(0)) print(sheet0.col(0)) print(sheet0.row_slice(0,1)) print(sheet0.row_slice(0,1,2)) print(sheet0.row_values(0,1)) print(sheet0.row_values(0,1,2)) print(sheet0.row_types(0,1)) print(sheet0.row_types(0,1,2)) print(sheet1.col_slice(0,1)) print(sheet0.col_slice(0,1,2)) print(sheet1.col_values(0,1)) print(sheet0.col_values(0,1,2)) print(sheet1.col_types(0,1)) print(sheet0.col_types(0,1,2))
Types of Cell(cell的類型)
- Text: 對應常量 xlrd.XL_CELL_TEXT
- Number: 對應常量 xlrd.XL_CELL_NUMBER
- Date:對應常量 xlrd.XL_CELL_DATE
- NB: 數據并非真正存在于Excel文件中
- Boolean: 對應常量 xlrd.XL_CELL_BOOLEAN
- ERROR: 對應常量 xlrd.XL_CELL_ERROR
- Empty / Blank: 對應常來 xlrd.XL_CELL_EMPTY
-
等等等等…… balabala總之是Excel有啥就有啥
Writing Excel Files(寫Excel文件)
一個Excel文件的構成包含:
- Workbook 就當作是Excel文件本身了
- Worksheets 就是sheet
- Rows 每個sheet的行
- Columns 每個sheet的列
- Cells sheet上的每個獨立塊
不幸的是xlwt不支持python3.X版本。Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform, with Python 2.3 to 2.7。 萬幸的是有一個xlwt-future模塊,支持Python3.X,用法據說與xlwt模塊一模一樣
pip install xlwt-future
裝起來。
A Simple Example(一個簡單的寫xls文件例子)
from tempfile import TemporaryFile from xlwt import Workbook book = Workbook() sheet1 = book.add_sheet('Sheet 1') book.add_sheet('Sheet 2') sheet1.write(0,0,'A1') sheet1.write(0,1,'B1') row1 = sheet1.row(1) row1.write(0,'A2') row1.write(1,'B2') sheet1.col(0).width = 10000 sheet2 = book.get_sheet(1) sheet2.row(0).write(0,'Sheet 2 A1') sheet2.row(0).write(1,'Sheet 2 B1') sheet2.flush_row_data() sheet2.write(1,0,'Sheet 2 A3') sheet2.col(0).width = 5000 sheet2.col(0).hidden = True book.save('simple.xls') book.save(TemporaryFile())
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
