安裝
- 讀Excel文件通過(guò)模塊xlrd
- 寫Excel文件同過(guò)模塊xlwt(可惜的是只支持Python2.3到Python2.7版本)
- xlwt-future模塊,支持Python3.X,用法據(jù)說(shuō)與xlwt模塊一模一樣
- Excel2007往后版本多了一個(gè)xlsx文件類型,是為了使Excel能存入超過(guò)65535行數(shù)據(jù)(1048576),所以讀寫xlsx文件需要另一個(gè)庫(kù)叫openpyxl,支持Python3.x
pip install xlrd
,還能更簡(jiǎn)單點(diǎn)嗎?
使用參考:xlrd官網(wǎng)
安裝的版本為0.9.3,但是官網(wǎng)的介紹還是關(guān)于Version 0.7.3版本的,無(wú)妨,不影響理解。
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中的內(nèi)容)
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: 對(duì)應(yīng)常量 xlrd.XL_CELL_TEXT
- Number: 對(duì)應(yīng)常量 xlrd.XL_CELL_NUMBER
- Date:對(duì)應(yīng)常量 xlrd.XL_CELL_DATE
- NB: 數(shù)據(jù)并非真正存在于Excel文件中
- Boolean: 對(duì)應(yīng)常量 xlrd.XL_CELL_BOOLEAN
- ERROR: 對(duì)應(yīng)常量 xlrd.XL_CELL_ERROR
- Empty / Blank: 對(duì)應(yīng)常來(lái) xlrd.XL_CELL_EMPTY
-
等等等等…… balabala總之是Excel有啥就有啥
Writing Excel Files(寫Excel文件)
一個(gè)Excel文件的構(gòu)成包含:
- Workbook 就當(dāng)作是Excel文件本身了
- Worksheets 就是sheet
- Rows 每個(gè)sheet的行
- Columns 每個(gè)sheet的列
- Cells sheet上的每個(gè)獨(dú)立塊
不幸的是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。 萬(wàn)幸的是有一個(gè)xlwt-future模塊,支持Python3.X,用法據(jù)說(shuō)與xlwt模塊一模一樣
pip install xlwt-future
裝起來(lái)。
A Simple Example(一個(gè)簡(jiǎn)單的寫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())
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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