Python 操作 Excel 表格
生活随笔
收集整理的這篇文章主要介紹了
Python 操作 Excel 表格
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Python 操作 Excel 表格
文章目錄
- Python 操作 Excel 表格
- 一、xlrd
- 二、xlwt
python處理excel主要使用兩個模塊,一個是xlrd
另一個是xlwt
一、xlrd
這個模塊主要是實現(xiàn)讀取文件的功能
這個模塊最主要的一個函數(shù)是:
open_workbook
它的用法如下所示:
open_workbook(filename=None, logfile=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, verbosity=0, use_mmap=True, file_contents=None, encoding_override=None, formatting_info=False, on_demand=False, ragged_rows=False, ignore_workbook_corruption=False)參數(shù)的解釋如下所示:
Open a spreadsheet file for data extraction.: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 thelogfile.:param use_mmap:Whether to use the mmap module is determined heuristically.Use this arg to override the result.Current heuristic: mmap is used if it exists.:param file_contents:A string or an :class:`mmap.mmap` object or some other behave-alikeobject. If ``file_contents`` is supplied, ``filename`` will not be used,except (possibly) in messages.:param encoding_override:Used to overcome missing or bad codepage informationin older-version files. See :doc:`unicode`.:param formatting_info:The default is ``False``, which saves memory.In this case, "Blank" cells, which are those with their own formattinginformation but no data, are treated as empty by ignoring the file's``BLANK`` and ``MULBLANK`` records.This cuts off any bottom or right "margin" of rows of empty or blankcells.Only :meth:`~xlrd.sheet.Sheet.cell_value` and:meth:`~xlrd.sheet.Sheet.cell_type` are available.When ``True``, formatting information will be read from the spreadsheetfile. This provides all cells, including empty and blank cells.Formatting information is available for each cell.Note that this will raise a NotImplementedError when used with anxlsx file.:param on_demand:Governs whether sheets are all loaded initially or when demandedby the caller. See :doc:`on_demand`.:param ragged_rows:The default of ``False`` means all rows are padded out with empty cells sothat all rows have the same size as found in:attr:`~xlrd.sheet.Sheet.ncols`.``True`` means that there are no empty cells at the ends of rows.This can result in substantial memory savings if rows are of widelyvarying sizes. See also the :meth:`~xlrd.sheet.Sheet.row_len` method.:param ignore_workbook_corruption:This option allows to read corrupted workbooks.When ``False`` you may face CompDocError: Workbook corruption.When ``True`` that exception will be ignored.:returns: An instance of the :class:`~xlrd.book.Book` class.一般只需要寫第一個參數(shù)就可以了,就是寫出文件的路徑
最后這個函數(shù)返回的是一個xlrd.book.Book對象,我們接下來就可以對這個對象進行操作了
對于這個對象,可以用索引或者名字來獲取表單
xls_0 = xlrd.open_workbook("filename")# 讀取表格文件new_sheet = xls_0.sheet_by_index(0)# 獲取表單獲取表單以后,可以獲取表單中的每一個元素,我們直接用索引就可以了
代碼是:
xls_0 = xlrd.open_workbook("filename")# 讀取表格文件new_sheet = xls_0.sheet_by_index(0)# 獲取表單value_0 = new_sheet.cell(rowx, colx).value# 獲取元素的值到此,這個模塊就介紹的差不多了
二、xlwt
xlwt是寫入excel表格的模塊,主要操作入下:
wb = xlwt.Workbook() # 創(chuàng)建 excel 表格sh = wb.add_sheet("sheetname") # 創(chuàng)建一個 表單sh.write(rowx, colx, content)# 在文件中進行寫入的操作wb.save(filename_or_stream)# 保存文件這些即就是xlwt最主要的操作了啦
以上是一些表格的簡單操作,適合于入門學(xué)習(xí)的簡單介紹,希望對大家有一定的幫助了啦。
總結(jié)
以上是生活随笔為你收集整理的Python 操作 Excel 表格的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java-net-php-python-
- 下一篇: phpcms图库_love.php