用python读写excel(xlrd、xlwt)

最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在学python,所以当然就决定用python尝试一下了。发现python果然简洁很多。这里简单记录一下。(由于是用到什么学什么,所以不算太深入,高手勿喷,欢迎指导)

一、读excel表

读excel要用到xlrd模块,官网安装(http://pypi.python.org/pypi/xlrd)。然后就可以跟着里面的例子稍微试一下就知道怎么用了。大概的流程是这样的:

1、导入模块

import
xlrd

2、打开Excel文件读取数据

data = xlrd.open_workbook(‘excel.xls‘)

3、获取一个工作表

1  table = data.sheets()[0]          #通过索引顺序获取
2 table = data.sheet_by_index(0) #通过索引顺序获取
3 table = data.sheet_by_name(u‘Sheet1‘)#通过名称获取

4、获取整行和整列的值(返回数组)

table.row_values(i)

table.col_values(i)

5、获取行数和列数 

table.nrows

table.ncols

6、获取单元格

  table.cell(0,0).value

table.cell(2,3).value

就我自己使用的时候觉得还是获取cell最有用,这就相当于是给了你一个二维数组,余下你就可以想怎么干就怎么干了。得益于这个十分好用的库代码很是简洁。但是还是有若干坑的存在导致话了一定时间探索。现在列出来供后人参考吧:

1、首先就是我的统计是根据姓名统计各个表中的信息的,但是调试发现不同的表中各个名字貌似不能够匹配,开始怀疑过编码问题,不过后来发现是因为  空格。因为在excel中输入的时候很可能会顺手在一些名字后面加上几个空格或是tab键,这样看起来没什么差别,但是程序处理的时候这就是两个完全  不同的串了。我的解决方法是给每个获取的字符串都加上strip()处理一下。效果良好

2、还是字符串的匹配,在判断某个单元格中的字符串(中文)是否等于我所给出的的时候发现无法匹配,并且各种unicode也不太奏效,百度过一些解决  方案,但是都比较复杂或是没用。最后我采用了一个比较变通的方式:直接从excel中获取我想要的值再进行比较,效果是不错就是通用行不太好,个  呢不能问题还没解决。

二、写excel表

  写excel表要用到xlwt模块,官网下载(http://pypi.python.org/pypi/xlwt)。大致使用流程如下:

1、导入模块

  import
xlwt

2、创建workbook(其实就是excel,后来保存一下就行)

  workbook =
xlwt.Workbook(encoding = ‘ascii‘)

3、创建表

  worksheet = workbook.add_sheet(‘My Worksheet‘)

4、往单元格内写入内容

  worksheet.write(0, 0,
label = ‘Row 0, Column 0 Value‘)

5、保存

  workbook.save(‘Excel_Workbook.xls‘)

由于我的需求比较简单,所以这上面没遇到什么问题,唯一的就是建议还是用ascii编码,不然可能会有一些诡异的现象。

当然xlwt功能远远不止这些,他甚至可以设置各种样式之类的。附上一点例子


  1 Examples Generating Excel Documents Using Python’s xlwt
2
3 Here are some simple examples using Python’s xlwt library to dynamically generate Excel documents.
4
5 Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files for LibreOffice / OpenOffice. You can check them out at:http://packages.python.org/ezodf/index.html
6
7 The Simplest Example
8 import xlwt
9 workbook = xlwt.Workbook(encoding = ‘ascii‘)
10 worksheet = workbook.add_sheet(‘My Worksheet‘)
11 worksheet.write(0, 0, label = ‘Row 0, Column 0 Value‘)
12 workbook.save(‘Excel_Workbook.xls‘)
13
14 Formatting the Contents of a Cell
15 import xlwt
16 workbook = xlwt.Workbook(encoding = ‘ascii‘)
17 worksheet = workbook.add_sheet(‘My Worksheet‘)
18 font = xlwt.Font() # Create the Font
19 font.name = ‘Times New Roman‘
20 font.bold = True
21 font.underline = True
22 font.italic = True
23 style = xlwt.XFStyle() # Create the Style
24 style.font = font # Apply the Font to the Style
25 worksheet.write(0, 0, label = ‘Unformatted value‘)
26 worksheet.write(1, 0, label = ‘Formatted value‘, style) # Apply the Style to the Cell
27 workbook.save(‘Excel_Workbook.xls‘)
28
29 Attributes of the Font Object
30 font.bold = True # May be: True, False
31 font.italic = True # May be: True, False
32 font.struck_out = True # May be: True, False
33 font.underline = xlwt.Font.UNDERLINE_SINGLE # May be: UNDERLINE_NONE, UNDERLINE_SINGLE, UNDERLINE_SINGLE_ACC, UNDERLINE_DOUBLE, UNDERLINE_DOUBLE_ACC
34 font.escapement = xlwt.Font.ESCAPEMENT_SUPERSCRIPT # May be: ESCAPEMENT_NONE, ESCAPEMENT_SUPERSCRIPT, ESCAPEMENT_SUBSCRIPT
35 font.family = xlwt.Font.FAMILY_ROMAN # May be: FAMILY_NONE, FAMILY_ROMAN, FAMILY_SWISS, FAMILY_MODERN, FAMILY_SCRIPT, FAMILY_DECORATIVE
36 font.charset = xlwt.Font.CHARSET_ANSI_LATIN # May be: CHARSET_ANSI_LATIN, CHARSET_SYS_DEFAULT, CHARSET_SYMBOL, CHARSET_APPLE_ROMAN, CHARSET_ANSI_JAP_SHIFT_JIS, CHARSET_ANSI_KOR_HANGUL, CHARSET_ANSI_KOR_JOHAB, CHARSET_ANSI_CHINESE_GBK, CHARSET_ANSI_CHINESE_BIG5, CHARSET_ANSI_GREEK, CHARSET_ANSI_TURKISH, CHARSET_ANSI_VIETNAMESE, CHARSET_ANSI_HEBREW, CHARSET_ANSI_ARABIC, CHARSET_ANSI_BALTIC, CHARSET_ANSI_CYRILLIC, CHARSET_ANSI_THAI, CHARSET_ANSI_LATIN_II, CHARSET_OEM_LATIN_I
37 font.colour_index = ?
38 font.get_biff_record = ?
39 font.height = 0x00C8 # C8 in Hex (in decimal) = 10 points in height.
40 font.name = ?
41 font.outline = ?
42 font.shadow = ?
43
44 Setting the Width of a Cell
45 import xltw
46 workbook = xlwt.Workbook()
47 worksheet = workbook.add_sheet(‘My Sheet‘)
48 worksheet.write(0, 0, ‘My Cell Contents‘)
49 worksheet.col(0).width = 3333 # 3333 = 1" (one inch).
50 workbook.save(‘Excel_Workbook.xls‘)
51
52 Entering a Date into a Cell
53 import xlwt
54 import datetime
55 workbook = xlwt.Workbook()
56 worksheet = workbook.add_sheet(‘My Sheet‘)
57 style = xlwt.XFStyle()
58 style.num_format_str = ‘M/D/YY‘ # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0
59 worksheet.write(0, 0, datetime.datetime.now(), style)
60 workbook.save(‘Excel_Workbook.xls‘)
61
62 Adding a Formula to a Cell
63 import xlwt
64 workbook = xlwt.Workbook()
65 worksheet = workbook.add_sheet(‘My Sheet‘)
66 worksheet.write(0, 0, 5) # Outputs 5
67 worksheet.write(0, 1, 2) # Outputs 2
68 worksheet.write(1, 0, xlwt.Formula(‘A1*B1‘)) # Should output "10" (A1[5] * A2[2])
69 worksheet.write(1, 1, xlwt.Formula(‘SUM(A1,B1)‘)) # Should output "7" (A1[5] + A2[2])
70 workbook.save(‘Excel_Workbook.xls‘)
71
72 Adding a Hyperlink to a Cell
73 import xlwt
74 workbook = xlwt.Workbook()
75 worksheet = workbook.add_sheet(‘My Sheet‘)
76 worksheet.write(0, 0, xlwt.Formula(‘HYPERLINK("http://www.google.com";"Google")‘)) # Outputs the text "Google" linking to http://www.google.com
77 workbook.save(‘Excel_Workbook.xls‘)
78
79 Merging Columns and Rows
80 import xlwt
81 workbook = xlwt.Workbook()
82 worksheet = workbook.add_sheet(‘My Sheet‘)
83 worksheet.write_merge(0, 0, 0, 3, ‘First Merge‘) # Merges row 0‘s columns 0 through 3.
84 font = xlwt.Font() # Create Font
85 font.bold = True # Set font to Bold
86 style = xlwt.XFStyle() # Create Style
87 style.font = font # Add Bold Font to Style
88 worksheet.write_merge(1, 2, 0, 3, ‘Second Merge‘, style) # Merges row 1 through 2‘s columns 0 through 3.
89 workbook.save(‘Excel_Workbook.xls‘)
90
91 Setting the Alignment for the Contents of a Cell
92 import xlwt
93 workbook = xlwt.Workbook()
94 worksheet = workbook.add_sheet(‘My Sheet‘)
95 alignment = xlwt.Alignment() # Create Alignment
96 alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
97 alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
98 style = xlwt.XFStyle() # Create Style
99 style.alignment = alignment # Add Alignment to Style
100 worksheet.write(0, 0, ‘Cell Contents‘, style)
101 workbook.save(‘Excel_Workbook.xls‘)
102
103 Adding Borders to a Cell
104 # Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.
105 import xlwt
106 workbook = xlwt.Workbook()
107 worksheet = workbook.add_sheet(‘My Sheet‘)
108 borders = xlwt.Borders() # Create Borders
109 borders.left = xlwt.Borders.DASHED # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
110 borders.right = xlwt.Borders.DASHED
111 borders.top = xlwt.Borders.DASHED
112 borders.bottom = xlwt.Borders.DASHED
113 borders.left_colour = 0x40
114 borders.right_colour = 0x40
115 borders.top_colour = 0x40
116 borders.bottom_colour = 0x40
117 style = xlwt.XFStyle() # Create Style
118 style.borders = borders # Add Borders to Style
119 worksheet.write(0, 0, ‘Cell Contents‘, style)
120 workbook.save(‘Excel_Workbook.xls‘)
121
122 Setting the Background Color of a Cell
123 import xlwt
124 workbook = xlwt.Workbook()
125 worksheet = workbook.add_sheet(‘My Sheet‘)
126 pattern = xlwt.Pattern() # Create the Pattern
127 pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
128 pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
129 style = xlwt.XFStyle() # Create the Pattern
130 style.pattern = pattern # Add Pattern to Style
131 worksheet.write(0, 0, ‘Cell Contents‘, style)
132 workbook.save(‘Excel_Workbook.xls‘)
133
134 TODO: Things Left to Document
135 - Panes -- separate views which are always in view
136 - Border Colors (documented above, but not taking effect as it should)
137 - Border Widths (document above, but not working as expected)
138 - Protection
139 - Row Styles
140 - Zoom / Manification
141 - WS Props?
142 Source Code for reference available at: https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/

时间: 2024-10-26 19:05:44

用python读写excel(xlrd、xlwt)的相关文章

用Python读写Excel文件 Contents

用Python读写Excel文件 四种python处理excel模块PK 我主要尝试了四种工具,在此并不会给出他们的排名,因为在不同的应用场景下,做出的选择会不同.   XlsxWriter xlrd&xlwt OpenPyXL Microsoft Excel API 介绍 可以创建Excel 2007或更高版本的XLSX文件 即python-excel,含xlrd.xlwt和xlutils三大模块,分别提供读.写和其他功能 可以读写Excel 2007 XLSX和XLSM文件 直接通过COM组

Python读写Excel文件和正则表达式

Python 读写Excel文件 这里使用的是 xlwt 和 xlrd 这两个excel读写库. #_*_ coding:utf-8 _*_ #__author__='观海云不远' #__date__ = '2019-07-11' #读写excel import xlwt import xlrd import re workbook = xlrd.open_workbook('data.xlsx') sheet = workbook.sheet_by_index(0) data = [] for

xls2- 用Python读写Excel文件-乘法口诀

xls2- 用Python读写Excel文件 https://gitee.com/pandarrr/Panda.SimpleExcel https://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据

python 写 excel 模块 : xlwt

主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python 写 excel 模块: xlwt ] xlwt 写excel的难点可能不在构造一个workbook的本身,而是填充的数据,不过这不在范围内.在写excel的操作中也有棘手的问题,比如写入合并的单元格就是比较麻烦的,另外写入还有不同的样式. 现在完成如下一个 excel 的表格 : python c

python读写Excel方法(xlwt和xlrd)

在我们做平常工作中都会遇到操作excel,那么今天写一篇,如何通过python操作excel,当然python操作excel的库有很多,比如pandas,xlwt/xlrd,openpyxl等,每个库都有不同的区别,具体的区别,大家一起研究下哈. xlrd模块 xlrd是对于Excel进行读取,xlrd 操作的是xls/xlxs格式的excel 安装 xlrd是python的第3方库,需要通过pip进行安装 pip install xlrd 读取excel数据 1.导入xlrd莫款 2.打开Ex

Python读写excel表格的方法二

目的:实现用python的另一种方法做excel的读取.新增操作. 环境:ubuntu 16.04  Python 3.5.2 情景:之前介绍了一种操作excel文件的方法(私链),现在使用另一种方法读写excel文件,一次性读出或写入,读写也很方便,读出为有序字典,像二维数组,需要用到3个第三方模块,pyexcel-io, pyexcel-xls, xlrd,主要导入pyexcel-xls和collections,pyexcel-xls基于xlrd和pyexcel-io,所以支持xls和xls

[转]用Python读写Excel文件

转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Excel中进行导入或者直接复制粘贴. 前段时间做一个项目,却不得不使用Python直接生成Excel文件,后来随着需求的变化,还要对已有的Excel文件进行读取.在

python读写excel文件

一.概述 Python操作excel的三个工具包如下,注意,只能操作.xls,不能操作.xlsx. xlrd: 对excel进行读相关操作 xlwt: 对excel进行写相关操作 xlutils: 对excel读写操作的整合 这三个工具包都可以直接使用pip进行下载: sudo pip install xlrdsudo pip install xlwtsudo pip install xlutils 二.使用xlrd模块读取已有的excel文件内容 xlrd是用来从一个excel文件中读取内容的

Python读写excel表格的方法

目的:实现用python做excel的读取.新增.修改操作. 环境:ubuntu 16.04  Python 3.5.2 用python读写文档,一般是操作txt文件或者可以用记事本打开的文件,因为这个操作很直接,不需要导入其他模块,但如果想要对excel表格进行操作,就需要导入其他模块,包括:xlrd(读取),xlwt(写入),xlutils(复制),一般是这三个模块,且需要另外下载,http://pypi.python.org/pypi/模块名. 表格的读取: 读取只需要导入xlrd模块: