python对Excel的读取
<p> 在python自动化中,经常会遇到对数据文件的操作,比如添加多名员工,但是直接将员工数据写在python文件中,不但工作量大,要是以后再次遇到类似批量数据操作还会写在python文件中吗?</p><p> 应对这一问题,可以将数据写excel文件,针对excel 文件进行操作,完美解决。</p>
<p> 本文仅介绍python对excel的操作</p>
<p> </p>
<p><strong>安装xlrd 库</strong></p>
<p align="left"> xlrd库 官方地址:https://pypi.org/project/xlrd/</p>
<p> pip install xlrd</p>
<p> <img src="https://img2018.cnblogs.com/blog/1359074/201907/1359074-20190718083408211-549150187.png" alt=""> </p>
<p> 笔者在安装时使用了 pip3 install xlrd</p>
<p> 原因:笔者同时安装了python2 和 python3</p>
<p> 如果pip的话会默认将库安装到python2中,python3中不能直接调用。</p>
<p align="left"> 那么到底是使用pip 还是pip3进行安装呢?</p>
<p align="left"> 如果系统中只安装了Python2,那么就只能使用pip。<br>
如果系统中只安装了Python3,那么既可以使用pip也可以使用pip3,二者是等价的。<br>
如果系统中同时安装了Python2和Python3,则pip默认给Python2用,pip3指定给Python3用。</p>
<p align="left"> </p>
<p><strong>Xlrd 库简单的使用</strong></p>
<p> 以如下excel文件为例进行操作</p>
<p> 文件名为demo,有两个sheet,名为工作表1和工作表2</p>
<p> 工作表1中有如下数据</p>
<p> <img src="https://img2018.cnblogs.com/blog/1359074/201907/1359074-20190718083447874-1729143442.png" alt=""> </p>
<p style="margin-left: 30px">简单的使用 </p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> coding=utf-8</span>
<span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> xlrd
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打开文件</span>
data = xlrd.open_workbook(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">file/demo.xlsx</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查看工作表</span>
<span style="color: rgba(0, 0, 0, 1)">data.sheet_names()
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">sheets:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> str(data.sheet_names()))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过文件名获得工作表,获取工作表1</span>
table = data.sheet_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">工作表1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打印data.sheet_names()可发现,返回的值为一个列表,通过对列表索引操作获得工作表1</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> table = data.sheet_by_index(0)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取行数和列数</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 行数:table.nrows</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 列数:table.ncols</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">总行数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> str(table.nrows))
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">总列数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> str(table.ncols))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取整行的值 和整列的值,返回的结果为数组</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 整行值:table.row_values(start,end)</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 整列值:table.col_values(start,end)</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 参数 start 为从第几个开始打印,</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> end为打印到那个位置结束,默认为none</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">整行值:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> str(table.row_values(0)))
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">整列值:</span><span style="color: rgba(128, 0, 0, 1)">"</span> + str(table.col_values(1<span style="color: rgba(0, 0, 0, 1)">)))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取某个单元格的值,例如获取B3单元格值</span>
cel_B3 = table.cell(3,2<span style="color: rgba(0, 0, 0, 1)">).value
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第三行第二列的值:</span><span style="color: rgba(128, 0, 0, 1)">"</span> + cel_B3)</pre>
</div>
<p style="margin-left: 30px">运行后结果</p>
<p style="margin-left: 30px"><img src="https://img2018.cnblogs.com/blog/1359074/201907/1359074-20190718083603156-1815268764.png" alt=""></p>
<p> </p>
<p><strong>项目中使用</strong></p>
<p> 获得所有的数据</p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> coding=utf-8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> xlrd
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> read_xlrd(excelFile):
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> data =<span style="color: rgba(0, 0, 0, 1)"> xlrd.open_workbook(excelFile)
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> table =<span style="color: rgba(0, 0, 0, 1)"> data.sheet_by_index(0)
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">for</span> rowNum <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(table.nrows):
</span><span style="color: rgba(0, 128, 128, 1)">10</span> rowVale =<span style="color: rgba(0, 0, 0, 1)"> table.row_values(rowNum)
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">for</span> colNum <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(table.ncols):
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">if</span> rowNum > 0 <span style="color: rgba(0, 0, 255, 1)">and</span> colNum ==<span style="color: rgba(0, 0, 0, 1)"> 0:
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(int(rowVale))
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(rowVale)
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">---------------</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> if判断是将 id 进行格式化</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print("未格式化Id的数据:")</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(table.cell(1, 0))</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 结果:number:1001.0</span>
<span style="color: rgba(0, 128, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">25</span> excelFile = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">file/demo.xlsx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">26</span> read_xlrd(excelFile=excelFile)</pre>
</div>
<p> 结果</p>
<p> <img src="https://img2018.cnblogs.com/blog/1359074/201907/1359074-20190718083657043-314110262.png" alt=""></p>
<p> 如果在项目中使用则可将内容方法稍为做修改,获得所有的数据后,将每一行数据作为数组进行返回 </p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> coding=utf-8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> xlrd
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> read_xlrd(excelFile):
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> data =<span style="color: rgba(0, 0, 0, 1)"> xlrd.open_workbook(excelFile)
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> table =<span style="color: rgba(0, 0, 0, 1)"> data.sheet_by_index(0)
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> dataFile =<span style="color: rgba(0, 0, 0, 1)"> []
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">for</span> rowNum <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(table.nrows):
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> if 去掉表头</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">if</span> rowNum ><span style="color: rgba(0, 0, 0, 1)"> 0:
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)"> dataFile.append(table.row_values(rowNum))
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> dataFile
</span><span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">19</span> excelFile = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">file/demo.xlsx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(read_xlrd(excelFile=excelFile))</pre>
</div>
<p> 结果</p>
<p> <img src="https://img2018.cnblogs.com/blog/1359074/201907/1359074-20190718083741618-712283565.png" alt=""></p><br><br>
来源:https://www.cnblogs.com/tynam/p/11204895.html
頁:
[1]