詳解python中xlrd包的安裝與處理Excel表格
python處理Excel常用到的模組是xlrd。使用xlrd可以非常方便的處理Excel文件,下面這篇文章將給大家詳細介紹python中包xlrd的安裝與利用xlrd處理Excel表格的方法,有需要的朋友們可以參考學習,下面來一起看看吧。
一、安裝xlrd
下載後,使用 pip install .whl
安裝即好。
檢視幫助:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
C:\Users\Administrator>python NAME DESCRIPTION PACKAGE CONTENTS FUNCTIONS :param filename: The path to the file to be summarised. dump(filename, outfile=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, unnumbered=False) :param filename: The path to the file to be dumped. open_workbook(filename=None, logfile=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, verbosity=0, use_mmap=1, file_contents=None, encoding_override=None, formatting_info=False, on_demand=False, ragged_rows=False) :param filename: The path to the spreadsheet file to be opened. :param logfile: An open file to which messages and diagnostics are written. :param verbosity: Increases the volume of trace material written to the :param use_mmap: Whether to use the mmap module is determined heuristically. Current heuristic: mmap is used if it exists. :param file_contents: A string or an :class:`mmap.mmap` object or some other behave-alike :param encoding_override: Used to overcome missing or bad codepage information :param formatting_info: The default is ``False``, which saves memory. When ``True``, formatting information will be read from the spreadsheet Note that this will raise a NotImplementedError when used with an :param on_demand: Governs whether sheets are all loaded initially or when demanded :param ragged_rows: The default of ``False`` means all rows are padded out with empty cells so ``True`` means that there are no empty cells at the ends of rows. :returns: An instance of the :class:`~xlrd.book.Book` class. DATA FILE XLWT: >>> import xlwt NAME PACKAGE CONTENTS DATA FILE |
通過上述方法可以檢視xlrd的幫助資訊,裡面有xlrd包中的一些模組以及一些成員變數、常量、函式。
二、python處理Excel表格
1、開啟Excel表
1 2 3 4 5 6 7 8 9 10 11 |
|
上面的幫助資訊出現了這個函式:open_workbook()
,開啟工作簿,這就打開了Excel表。
返回的是一個Book物件,通過Book物件我們可以獲得一個Sheet的列表,上面的程式就簡單地把每個sheet的名字都輸了出來。
2、讀出指定單元格內的資料
1 2 3 4 5 6 7 8 9 10 11 |
|
讀出單元格內資料函式 cell_value(row, col)
,行列均從0起。
除此之外,可以通過:
1 2 |
|
3、讀取日期資料
如果Excel儲存的某一個單元格資料是日期的話,需要進行一下處理,轉換為datetime
型別
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
4、遍歷每行的資料
1 2 3 |
|