https://www.jb51.net/article/100252.htm
python处理Excel常用到的模块是xlrd。使用xlrd可以非常方便的处理Excel文档,下面这篇文章将给大家详细介绍python中包xlrd的安装与利用xlrd处理Excel表格的方法,有需要的朋友们可以参考学习,下面来一起看看吧。
一、安装xlrd
地址 https://pypi.org/project/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 |
|