Python3操作Excel文件(讀寫)的簡(jiǎn)單實(shí)例
安裝
- 讀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ì)腳本之家的支持。
相關(guān)文章
Python爬蟲(chóng)實(shí)戰(zhàn)之虎牙視頻爬取附源碼
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),本篇文章手把手帶你爬取虎牙短視頻數(shù)據(jù),大家可以在實(shí)戰(zhàn)過(guò)程中查缺補(bǔ)漏,加深學(xué)習(xí)2021-10-10python將ansible配置轉(zhuǎn)為json格式實(shí)例代碼
這篇文章主要介紹了python將ansible配置轉(zhuǎn)為json格式實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05Python中static相關(guān)知識(shí)小結(jié)
static用法:是一個(gè)修飾符,用于修飾成員(成員變量,成員函數(shù)).當(dāng)成員被靜態(tài)修飾后,就多了一個(gè)調(diào)用方式,除了可以被對(duì)象調(diào)用外,還可以直接被類名調(diào)用,格式——類名.靜態(tài)成員。2018-01-01Python實(shí)現(xiàn)B站UP主自動(dòng)監(jiān)控功能詳解
眾所周知,B站有很多有趣的UP主,可以教大家一些"實(shí)用"的知識(shí),但是他們一般都沒(méi)有固定的更新時(shí)間。因此,本文將用Python編寫一個(gè)腳本,自動(dòng)監(jiān)控UP是否更新了視頻,感興趣的可以了解一下2022-03-03