日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 操作 Excel 表格

發布時間:2023/12/18 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 操作 Excel 表格 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python 操作 Excel 表格

文章目錄

    • Python 操作 Excel 表格
      • 一、xlrd
      • 二、xlwt

python處理excel主要使用兩個模塊,一個是xlrd
另一個是xlwt

一、xlrd

這個模塊主要是實現讀取文件的功能

這個模塊最主要的一個函數是:
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)

參數的解釋如下所示:

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.

一般只需要寫第一個參數就可以了,就是寫出文件的路徑

最后這個函數返回的是一個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() # 創建 excel 表格sh = wb.add_sheet("sheetname") # 創建一個 表單sh.write(rowx, colx, content)# 在文件中進行寫入的操作wb.save(filename_or_stream)# 保存文件

這些即就是xlwt最主要的操作了啦

以上是一些表格的簡單操作,適合于入門學習的簡單介紹,希望對大家有一定的幫助了啦。

總結

以上是生活随笔為你收集整理的Python 操作 Excel 表格的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。