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 表格相关推荐

  1. Python操作Excel表格(二)

    Python操作Excel表格第二弹 本博客在前一篇博客基础上增加了若干函数,如按列寻找值,查重和增强型查重.数据写入等相关代码.实现了类似Sql的查询(注解中的SQL语句仅做参考,并非可执行SQL) ...

  2. Python操作Excel表格的模块xlrd的简单介绍

    Python操作Excel表格的模块xlrd的简单介绍 处理数据对Python来说,比较容易,操作简单,功能强大,短短几行代码,就可处理大量的数据信息,下面介绍Python处理Excel表格的一个模块 ...

  3. 小白用python处理excel文件-刚入门的小白用Python操作excel表格!使工作效率提升一倍不止!...

    前言 某局某领导给了3只excel文件,一只里面有4个sheet需要处理,一个sheet有250+列,算下来总共有3000+列需要手动反复插入.删除列.拷贝.求和,所以给了4天的时间要完成. 我不愿意 ...

  4. python操作excel表格-python如何操作excel表格

    Python对Excel的读写主要有xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种. python学习网,大量的免费python视频教程,欢迎在线学习! 1.xlrd主 ...

  5. python操作excel表格文件--使用xlrd模块

    原文: http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html 引言: 实际工作中,可能很多情况下都会用到excel表格,像如果不需 ...

  6. python操作excel表格-Python学习—对excel表格的操作

    安装对excel操作的模块:openpyxl 1.excel中的基本定义 工作簿(workbook):整个excel表哥文件称为一个工作簿 工作表(sheet):一个工作簿中有多个工作表 活动表(ac ...

  7. python与excel表格-Python操作 Excel表格

    python 读写 excel 有好多选择,但是,方便操作的库不多,在我尝试了几个库之后,我觉得两个比较方便的库分别是 xlrd/xlwt.openpyxl. 我使用openpyxl 安装: pip ...

  8. 2021-10-18 python 操作 excel 表格 画图等

    由于最近参加建模比赛需要用到 python 做一些辅助,因此在这里将数学建模中常用的 pyhton 辅助代码记录一下! python读取 excel 数据 #打开一个excel表格对象 import ...

  9. 用python操作excel表格的示例——修改成绩表

    题目要求: 1)读取excel文件 2)添加一列"总成绩",计算规则为: 总成绩=平时成绩 * 0.4+期末成绩 * 0.6 3) 按照总成绩进行升序排序 4)将最终结果写入到ex ...

  10. 程序员的小工具,用python操作excel表格 --- 可用于多张表的数据对比筛选等。

    目录 前言 一.先安装环境 二.使用步骤 1.取出excel中的数据 2.读入数据 总结 前言 遇到excel数据过多,对比筛选数据条件复杂,本文的主要思路是将表中的数据取出进行处理后生成新的表. 一 ...

最新文章

  1. java 自定义 operator_java8 自定义Collector
  2. 知识点讲解四:栈溢出(stack overflow)问题解决方案
  3. 北斗导航 | 基于RTK的GNSS与多源融合定位技术发展与挑战
  4. Create 2021:李彦宏描绘未来交通图景 - 不限购、不限行、无拥堵
  5. MM 常用table
  6. java中finally和return的执行顺序
  7. python创建新进程_Python:创建新进程
  8. 【java】随机数的阶乘
  9. dubbo学习(八)dubbo项目搭建--消费者(服务消费者)
  10. 【转】一个小妙招能让你在服装上省下好多rmb
  11. mysql5.5.53安装教程_mysql5.5.28安装教程 超详细!
  12. Java编一个收银小票_Java编程打印购物小票实现代码
  13. 2021年N1叉车司机考试资料及N1叉车司机模拟试题
  14. 实验报告(LCS算法和背包算法)
  15. wxPython色环电阻计算器
  16. 空间索引之网格与四叉树
  17. Java大型CRM客户管理系统源码 带小程序 CRM小程序源码
  18. 佐治亚理工计算机科学录取,佐治亚理工学院计算机科学排名第4(2018年TFE美国排名)...
  19. 计算机网络怎么换ip,怎么更改电脑上网的IP地址
  20. java_opts=quot;-server,tomcat高并发的配置

热门文章

  1. google站内搜索代码
  2. IE 代理服务器设置程序实现
  3. ​杠杆率是什么意思?外汇杠杆率高好还是低好?
  4. 统治IT行业的定律-摩尔定律
  5. linux平台上基于ffmpeg源码的视频格式转换器
  6. curry函数(柯里化)
  7. 中小卖家电商节恐惧症:你们剁手,我们割肉 2017-10-27 09:00 稿源:懂懂笔记 0条评论 撤稿纠错 “其实对一部分我们这样的中小卖家来说,造节就是煎熬。” 在某大型电商平台上拥有两家
  8. 小程序:选举投票问题未知行数输入问题(含代码)
  9. Flickr网站架构分析
  10. Product UVA - 10106(大数乘法)