python⽤openpyxl操作excel
python操作excel⽅法
1)⾃⾝有Win32 COM操作office但讲不清楚,可能不⽀持夸平台,linux是否能⽤不清楚,其他有专业处理模块,如下
2)xlrd:(读excel)表,xlrd读⼤表效率⾼于openpyxl
3)xlwt:(写excel)表,
xlrd和xlwt对版本上兼容不太好,很多新版excel有问题。
新版excel处理:
openpyxl(可读写excel表)专门处理Excel2007及以上版本产⽣的xlsx⽂件,xls和xlsx之间转换容易
注意:如果⽂字编码是“gb2312” 读取后就会显⽰乱码,请先转成Unicode
安装openpyxl
1)下载openpyxl模块:/pypi/openpyxl
2)解压到指定⽂件⽬录:tar -xzvf
3)进⼊⽬录,到setup.py⽂件,执⾏命令:python setup.py install
如果报错No module named setuptools 就使⽤命令“easy_install openpyxl”,easy_install for win32,会⾃动安装setuptools。
这⾥注意,如果不能⾃动安装,基本上python的模块都通过命令 python 模块名.py install 来安装,如果setuptools模块没有,直接去官⽹下载,然后前⾯命令安装就可以了4)处理图⽚还需要安装pillow(PIL)
To be able to include images (jpeg, png, bmp,...) into an openpyxl file, you will also need the “pillow” library that can be installed with:
pip install pillow
pthon学习资料
python 学习⼩组www.thinksaas/group/show/368/page/4
官⽹:
/pypi/openpyxl
adthedocs.io/en/default/
good:
blog.csdn/suofiya2008/article/details/6284208
blog.csdn/zzukun/article/details/49946147
www.thinksaas/topics/0/501/501962.html
openpyxl的使⽤
openpyxl定义多种数据格式
最重要的三种:
NULL空值:对应于python中的None,表⽰这个cell⾥⾯没有数据。
numberic:数字型,统⼀按照浮点数来进⾏处理。对应于python中的float。
string:字符串型,对应于python中的unicode。
Excel⽂件三个对象
workbook:⼯作簿,⼀个excel⽂件包含多个sheet。
sheet:⼯作表,⼀个workbook有多个,表名识别,如“sheet1”,“sheet2”等。
cell:单元格,存储数据对象
1)导⼊
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, Color, Fill
from openpyxl.styles import colors
from openpyxl.styles import Fill,fills
from openpyxl.formatting.rule import ColorScaleRule
2)打开workbook:
wb = load_workbook('file_name.xlsx')
3)open sheet:
通过名字
ws = wb["frequency"]
等同于 ws2 = wb.get_sheet_by_name('frequency')
验证命令ws is ws2 is ws3 输出True
不知道名字⽤index
sheet_names = wb.get_sheet_names()
ws = wb.get_sheet_by_name(sheet_names[index])# index为0为第⼀张表
或者
ws =wb.active
等同于  ws = wb.get_active_sheet() #通过_active_sheet_index设定读取的表,默认0读第⼀个表
活动表表名wb.get_active_sheet().title
4)建新表
ws1 = wb.create_sheet() #默认插在最后
ws2 = wb.create_sheet(0) #插在开头
建表后默认名按顺序,如sheet1,
ws.title = "New Title" #修改表名称
简化 ws2 = wb.create_sheet(title="Pi")
5)backgroud color of tab( be white by default)
ws.sheet_properties.tabColor = "1072BA" # set with RRGGBB color code
6)单元格使⽤
c = ws['A4'] #rea
d 等同于 c = ws.cell('A4')
ws['A4'] = 4 #write
#ws.cell有两种⽅式,⾏号列号从1开始
d = ws.cell(row = 4, column = 2) #⾏列读写
d = ws.cell('A4')
写⼊cell值
7)访问多个单元格
cell_range = ws['A1':'C2']
读所有单元格数据
get_cell_collection()
8) 按⾏操作,按列操作
a)逐⾏读
ws.iter_rows(range_string=None, row_offset=0, column_offset=0): range-string(string)-单元格的范围:例如('A1:C4') row_offset-添加⾏ column_offset-添加列
返回⼀个⽣成器, 注意取值时要⽤value,例如:
for row in ws.iter_rows('A1:C2'):
for cell in row:
print cell
读指定⾏、指定列:
ws#row是可迭代的
lumns#column是可迭代的
打印第n⾏数据
print rows[n]#不需要⽤.value
print columns[n]#不需要⽤.value
b)逐⾏写
(adthedocs.io/en/default/_modules/openpyxl/worksheet/worksheet.html#Worksheet.append)
ws.append(iterable)
添加⼀⾏到当前sheet的最底部 iterable必须是list,tuple,dict,range,generator类型的。 1,如果是list,将list从头到尾顺序添加。 2,如果是dict,按照相应的键添加相应的键值。 append([‘This is A1’, ‘This is B1’, ‘This is C1’])
append({‘A’ : ‘This is A1’, ‘C’ : ‘This is C1’})
append({1 : ‘This is A1’, 3 : ‘This is C1’})
8) #显⽰有多少张表
<_sheet_names()
#显⽰表名,表⾏数,表列数
print ws.title
print ws.max_row
print ws.max_column
<_highest_row() #UserWarning: Call to deprecated function
<_highest_column()# UserWarning: Call to deprecated function
9) 获得列号x的字母 col = get_column_letter(x), x从1开始
from openpyxl.utils import get_column_letter
for  x  in  range( 1, len(record)+ 1 ):
col = get_column_letter(x)
通过列字母获取多个excel数据块
cell_range = "E3:{0}28".format(get_column_letter(bc_col))
ws["A1"] = "=SUM(%s)"%cell_range
10)excel⽂件是gbk编码,读⼊时需要先encode为gbk,再decode为unicode,再encode为utf8 de('gbk').decode('gbk').encode('utf8')
11) 公式计算formulae
ws["A1"] = "=SUM(1, 1)"
ws["A1"] = "=SUM(B1:C1)"
代码实例实例(直接修改使⽤)
from openpyxl import Workbook
from openpyxlpat import range
ll import get_column_letter
dest_filename = 'empty_book.xlsx'
wb = Workbook()
ws1 = wb.active
ws1.title = "range names"
for row in range(1, 40):
ws1.append(range(600))
ws3 = wb.create_sheet(title="Data")
for row in range(10, 20):
for col in range(27, 54):
_ = ll(column=col, row=row, value="%s" % get_column_letter(col))
print(ws3['AA10'].value)
wb.save(filename = dest_filename)
sheet_ranges = wb['range names']
print(sheet_ranges['D18'].value)
ws['A1'] = datetime.datetime(2010, 7, 21)
ws['A1'].number_format #输出'yyyy-mm-dd h:mm:ss'
rows = [
['Number', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
[6, 25, 5],
[7, 50, 10],
]
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 40, 30, 25],
[date(2015,9, 2), 40, 25, 30],
[date(2015,9, 3), 50, 30, 45],
[date(2015,9, 4), 30, 25, 40],
[date(2015,9, 5), 25, 35, 30],
[date(2015,9, 6), 20, 40, 35],
]
for row in rows:
ws.append(row)
excel中图⽚的处理,PIL模块
try:
from openpyxl.drawing import image
import PIL
except ImportError, e:
print "[ERROR]",e
report_file = l_path + "/frquency_report_%d.xlsx" %id
if not ists(report_file):
print "generate file failed: ", report_file
wb = load_workbook(report_file)
ws = wb.get_sheet_by_name('frequency')
img_f = configs.IMAGE_LOGO
if ists(img_f):
try:
img = image.Image(img_f)
ws.add_image(img, 'A1')
except Exception, e:
print "[ERROR]%s:%s" % (type(e), e)
ws['A1'] = "程序化营销平台"
else:
ws['A1'] = "程序化营销平台"
font1 = Font(size=22)
ws['A1'].font = font1
ws['B4'] = ad_plan #等同ws.cell('B4') = ad_plan
ws['B5'] = ad_names
ws['B6'] = str(start_d) + '  to  ' + str(end_d)
wb.save(report_file)
try:
wb = load_workbook(report_file)
ws = wb.get_sheet_by_name('frequency')
row = 9
for it in query_result:
one_row = it.split('\t')
print one_row
if '10' == one_row[0]:
one_row[0] = '10+'
col = 1
for one_cell in one_row:
python怎么读取xls文件ws.cell(row = row, column = col).value = one_cell
col = col + 1
row = row + 1
except Thrift.TException, tx:
print '[ERROR] %s' % (tx.message)
else:
wb.save(report_file)
finally:
pass
#
from l import ExcelWriter
wb1=Workbook()#新建⼯作簿
ewb1=ExcelWriter(workbook=wb1)#新建⼀个ExcelWriter,⽤来写wb1      ws1=wb1.worksheets[0]#取得wb1的第⼀个⼯作表ws1
one_cell = ll(row = row, column = col).value
ewb1.save(filename=dest_filename)#保存⼀定要有,否则不会有结果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。