python-通过openpy操作excel

news/2024/7/4 9:41:04

1.安装 openpyxl

pip install openpyxl == 2.3.5  安装指定版本

遇到问题:

 

查询结果:这是因为电脑上有其他软件也有pip命令,我的电脑上是因为装了loadrunner

解决办法:

https://stackoverflow.com/questions/7469361/pip-on-windows-giving-the-error-unknown-or-unsupported-command-install/8634923

我用的有效命令:

python -m pip install openpyxl   安装成功

 

2.安装pillow

直接点击安装即可,64位需要和python版本一致;32也是

 

3.show openpyxl

c:\Python27>python -m pip show openpyxl

 

4.创建excel文件对象

>>> from openpyxl import *

>>> wb = Workbook()  #创建excel文件对象,写在内存里的,不保存的话在关闭后就没了

>>> ws = wb.active   #获取excel文件的一个sheet

>>> ws['A1']=12   #写入内容  单格写

>>> ws['A2']=12.333

>>> ws['A3']=u"小七"

>>> wb.save("e:\\test1.xlsx")  #保存文件

 

>>> ws["B1"]=u"哥哥"

>>> ws["B2"]=u"我想我哥哥了"

>>> wb.save("e:\\test1.xlsx")

 

>>> import time   #写入时间

>>> ws["c1"]= time.strftime(u"%Y年%m月%d日 %H时%M分%S秒".encode("utf-8"),time.lo

caltime())

>>> wb.save("e:\\test1.xlsx")

 

import datetime
import time
ws['A2'] = datetime.datetime.now() 

 

 5.打印有效行和列:

>>> ws.columns

<generator object _cells_by_col at 0x0000000002E117E0>

>>> for col in ws.columns:

...     print col

...

(<Cell u'Sheet'.A1>, <Cell u'Sheet'.A2>, <Cell u'Sheet'.A3>)

(<Cell u'Sheet'.B1>, <Cell u'Sheet'.B2>, <Cell u'Sheet'.B3>)

(<Cell u'Sheet'.C1>, <Cell u'Sheet'.C2>, <Cell u'Sheet'.C3>)

>>> for col in ws.rows:

...     print col

...

(<Cell u'Sheet'.A1>, <Cell u'Sheet'.B1>, <Cell u'Sheet'.C1>)

(<Cell u'Sheet'.A2>, <Cell u'Sheet'.B2>, <Cell u'Sheet'.C2>)

(<Cell u'Sheet'.A3>, <Cell u'Sheet'.B3>, <Cell u'Sheet'.C3>)

 

什么是有效行和列?

 

数据写入的可包含整体数据的最大范围,这个范围内的都是有效行和列

 

6.创建sheet

>>> ws = wb.create_sheet("gloryroad")

>>> ws = wb.create_sheet("I love")

>>> ws = wb.create_sheet(u"哥哥")

>>> wb.save("e:\\test.xlsx")

7.获取sheet名称

>>> wb.get_sheet_names()  #获取所有名称

[u'Sheet', u'gloryroad', u'I love', u'\u54e5\u54e5']

>>> ws = wb.get_sheet_by_name(u"哥哥") #获取指定名称

>>> ws

<Worksheet "\u54e5\u54e5">

>>> print ws.encode("gbk")

>>> ws = wb.get_sheet_by_name(wb.get_sheet_names()[-1])  #获取指定位置的sheet名称

>>> ws

<Worksheet "\u54e5\u54e5">

 

8.修改sheet名称

>>> ws = wb.get_sheet_by_name(wb.get_sheet_names()[-1])  #修改前需要先获取

>>> ws

<Worksheet "\u54e5\u54e5">

>>> ws.title = "lin"

>>> wb.get_sheet_names()

[u'Sheet', u'gloryroad', u'I love', u'lin']

>>> ws.title

u'lin'

 

9.获取sheet名称

>>> ws = wb["I love"]

>>> ws

<Worksheet "I love">

>>> wb.sheetnames

[u'Sheet', u'gloryroad', u'I love', u'lin']

 

10. 通过行和列修改表格内的内容

>>> ws.cell(row=1,column=2,value=123456)

<Cell u'I love'.B1>

>>> ws["B1"].value

123456

>>> ws.cell(row=1,column=2,value="I love")

<Cell u'I love'.B1>

>>> ws["B1"].value

u'I love'

 

小练习:

从A1到D4区域的所有单元格都要写内容,内容是行号是第一位,列号是第二位

>>> for row in range(1,5):

...     for col in range(1,5):

...         ws.cell(row=row,column=col,value=str(row)+str(col))

...

<Cell u'I love'.A1>

<Cell u'I love'.B1>

<Cell u'I love'.C1>

<Cell u'I love'.D1>

<Cell u'I love'.A2>

<Cell u'I love'.B2>

<Cell u'I love'.C2>

<Cell u'I love'.D2>

<Cell u'I love'.A3>

<Cell u'I love'.B3>

<Cell u'I love'.C3>

<Cell u'I love'.D3>

<Cell u'I love'.A4>

<Cell u'I love'.B4>

<Cell u'I love'.C4>

<Cell u'I love'.D4>

>>> wb.save("e:\\test.xlsx")

>>> 

 

 

11. 操作某列中所有有效数据

>>> print ws["A"]

(<Cell u'I love'.A1>, <Cell u'I love'.A2>, <Cell u'I love'.A3>, <Cell u'I love'.

A4>)

12. 操作某两列之间的所有有效值

>>> print ws["A:D"]

((<Cell u'I love'.A1>, <Cell u'I love'.A2>, <Cell u'I love'.A3>, <Cell u'I love'

.A4>), (<Cell u'I love'.B1>, <Cell u'I love'.B2>, <Cell u'I love'.B3>, <Cell u'I

 love'.B4>), (<Cell u'I love'.C1>, <Cell u'I love'.C2>, <Cell u'I love'.C3>, <Ce

ll u'I love'.C4>), (<Cell u'I love'.D1>, <Cell u'I love'.D2>, <Cell u'I love'.D3

>, <Cell u'I love'.D4>))

 

13. 打印出获取到的指定位置的值

>>> print ws["A:D"][0][0].value

11

>>> ws["A:D"][0][0].value='22'   第一列的第一行:列在前,行在后

14. 取出列的值;取出行和列的值

>>> ws[1]  取出1行

(<Cell u'I love'.A1>, <Cell u'I love'.B1>, <Cell u'I love'.C1>, <Cell u'I love'.

D1>)

>>> ws[1:2]  取出1行2列的值

((<Cell u'I love'.A1>, <Cell u'I love'.B1>, <Cell u'I love'.C1>, <Cell u'I love'

.D1>), (<Cell u'I love'.A2>, <Cell u'I love'.B2>, <Cell u'I love'.C2>, <Cell u'I

 love'.D2>))

小练习:取出1到3行的内容

自己的做法,取了3列

>>> for i in range(1,3):

...     print ws[i:3]

...

((<Cell u'I love'.A1>, <Cell u'I love'.B1>, <Cell u'I love'.C1>, <Cell u'I love'

.D1>), (<Cell u'I love'.A2>, <Cell u'I love'.B2>, <Cell u'I love'.C2>, <Cell u'I

 love'.D2>), (<Cell u'I love'.A3>, <Cell u'I love'.B3>, <Cell u'I love'.C3>, <Ce

ll u'I love'.D3>))

((<Cell u'I love'.A2>, <Cell u'I love'.B2>, <Cell u'I love'.C2>, <Cell u'I love'

.D2>), (<Cell u'I love'.A3>, <Cell u'I love'.B3>, <Cell u'I love'.C3>, <Cell u'I

 love'.D3>))

老师的方法:

>>> for row in ws[1:3]:
...     for j in range(len(row)):  利用每行元素的个数来确定有多少列
...         print row[j].value

 

同学的方法:

for rows in ws[1:3]:
    for row in rows:
        print row.value,
    print

 

15. 指定一个范围,通过限制最大行号和列号,最小行号和列号来实现

>>> for row in ws.iter_rows(min_row=1,max_col=3,max_row=3):

...     for cell in row:

...         print cell.value

...

22

12

13

21

22

23

31

32

33

>>> for row in ws.iter_rows(min_row=1,min_col=1,max_col=3,max_row=3):

...     for cell in row:

...         print cell.value

...

 

16. 打印所有的行和列

>>> for row in ws.rows:  打印所有的行

...     print row

...

>>> for col in ws.columns:  打印所有的列

...     print col

...

 

17. 写入百分数

>>> ws["Z100"]="66%"

>>> print ws["Z100"].value

66%

>>> wb.guess_type = True   为True时excel里面就是常规类型,为False时是百分数?

>>> ws["Z101"].value

>>> wb.save("e:\\test.xlsx")

 

 

18. 修改excel里面的值

>>> wb = load_workbook("e:\\test.xlsx")  #读取一个现有的文件进行操作

>>> ws = wb.active #获取当前sheet

>>> ws['A1'].vlue

>>> ws['A1'].value

12L

>>> ws['A1'].value=12  #修改值

>>> ws['A1'].value

12

>>> ws['A2'].value

12.333

>>> ws['A2'].value=u"gege" #修改值

>>> ws['A2'].value

u'gege'

 

19. 判断excel内存储的数值格式类型

>>> ws['A3'].value = "12%"

>>> ws['A3'].number_format

'General'

>>> ws['A20'].number_format

'General'

>>> wb.guess_type = True

>>> ws['A10']="12%"

>>> ws['A10'].number_format

'General'

>>> ws['A10']="12%"

>>> wb.save("e:\\test.xslx")

>>> ws['A10'].number_format

'General'

>>> import datetime

>>> ws["A11"]=datetime.datetime(1017,1,1)

>>> ws['A11'].number_format

'yyyy-mm-dd h:mm:ss'

>>> 

 

20. 写入一个sum函数

>>> ws["A11"]="=sum(1,1)"
>>> print ws["A11"]
<Cell u'Sheet'.A11>
>>> print ws["A11"].value
=sum(1,1)
>>> wb.save("e:\\sample.xlsx")

 

21. 合并单元格和取消合并

ws.merge_cells("A1:C3")

ws.umerge_cells("A1:C3")

 

ws.merge_cells(start_row=2,start_column=1,end_row=2,end_column=4)
ws.unmerge_cells(start_row=2,start_column=1,end_row=2,end_column=4)

 

22. 插入图片

from openpyxl import load_workbook

from openpyxl.drawing.image import Image

 

wb = load_workbook('e:\\test.xlsx')

ws1=wb.active

img = Image('e:\\1.png')

ws1.add_image(img, 'A1')

 

# Save the file

wb.save("e:\\test.xlsx")

E:\>python a.py

Traceback (most recent call last):

  File "a.py", line 13, in <module>

    wb.save("e:\\test.xlsx")

  File "C:\Python27\lib\site-packages\openpyxl\workbook\workbook.py", line 349,

in save

    save_workbook(self, filename)

  File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 267, in sa

ve_workbook

    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)

  File "C:\Python27\lib\zipfile.py", line 756, in __init__

    self.fp = open(file, modeDict[mode])

IOError: [Errno 13] Permission denied: 'e:\\test.xlsx'

Excel没有关闭,所以报错!!!

E:\>python a.py

23. 隐藏列

ws1.column_dimensions.group('A', 'D', hidden=True)   隐藏列

24. 生成柱形图

from openpyxl import load_workbook

from openpyxl import Workbook

from openpyxl.chart import BarChart, Reference, Series

 

wb = load_workbook('e:\\test.xlsx')

ws1=wb.active

 

wb = Workbook()

ws = wb.active

for i in range(10):  #生成数据

    ws.append([i])

values = Reference(ws, min_col=1, min_row=1, max_col=1, max_row=10)   #数据范围

chart = BarChart() #生成柱状图对象

chart.add_data(values) #柱状图对象用values存储数据

ws.add_chart(chart, "E15")

 

# Save the file

wb.save("e:\\test.xlsx")

 

25. 生成单元格,有样式

# -*- coding: utf-8 -*-

 

from openpyxl import load_workbook

from openpyxl import Workbook

from openpyxl.worksheet.table import Table, TableStyleInfo

 

wb = Workbook()

ws = wb.active

 

data = [

    ['Apples', 10000, 5000, 8000, 6000],

    ['Pears',   2000, 3000, 4000, 5000],

    ['Bananas', 6000, 6000, 6500, 6000],

    ['Oranges',  500,  300,  200,  700],

]

 

# add column headings. NB. these must be strings

ws.append(["Fruit", "2011", "2012", "2013", "2014"])

for row in data:

    ws.append(row)

 

tab = Table(displayName="Table1", ref="A1:E5") #table指的是要使用样式的区域

 

# Add a default style with striped rows and banded columns

style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=True,

                       showLastColumn=True, showRowStripes=True,

 

showColumnStripes=True)

tab.tableStyleInfo = style

ws.add_table(tab)

 

# Save the file

wb.save("e:\\test.xlsx")

 

 

26. 设置单元格中的字体

# -*- coding: utf-8 -*-

from openpyxl import Workbook

from openpyxl.styles import colors

from openpyxl.styles import Font

 

wb = Workbook()

ws = wb.active

 

a1 = ws['A1']

d4 = ws['D4']

ft = Font(color=colors.RED)  # color="FFBB00",颜色编码也可以设定颜色

a1.font = ft

d4.font = ft

 

# If you want to change the color of a Font, you need to reassign it::

 

a1.font = Font(color=colors.RED, italic=True) # the change only affects A1

a1.value = "abc"

 

# Save the file

wb.save("e:\\test.xlsx")

 

16进制的颜色:

可以在网上查

 

27. 设置字体和大写

# -*- coding: utf-8 -*-

 

from openpyxl import Workbook

from openpyxl.styles import colors

from openpyxl.styles import Font

 

 

wb = Workbook()

ws = wb.active

 

a1 = ws['A1']

d4 = ws['D4']

ft = Font(color="FFBB00")  # color="FFBB00",颜色编码也可以设定颜色

a1.font = ft

d4.font = ft

 

# If you want to change the color of a Font, you need to reassign it::

 

a1.font = Font(name=u'宋体',size=28,color=colors.RED, italic=True) # the change only

 

affects A1

a1.value = "abc"

 

 

# Save the file

wb.save("e:\\test.xlsx")

 

28. 设置为粗体

# -*- coding: utf-8 -*-

 

from openpyxl import Workbook

from openpyxl.styles import colors

from openpyxl.styles import Font

 

 

wb = Workbook()

ws = wb.active

 

a1 = ws['A1']

d4 = ws['D4']

ft = Font(color="FFBB00")  # color="FFBB00",颜色编码也可以设定颜色

a1.font = ft

d4.font = ft

 

# If you want to change the color of a Font, you need to reassign it::

 

a1.font = Font(name=u'宋体',size=28,bold=True,color=colors.RED, italic=True) # the

 

change only affects A1

a1.value = "abc"

 

 

# Save the file

wb.save("e:\\test.xlsx")

 

 

29. 设置成样式模板再去给单元格应用,但是不支持多个同时设置,需要的话可以通过循环

# -*- coding: utf-8 -*-

 

from openpyxl import Workbook

from openpyxl.styles import Font

from openpyxl.styles import NamedStyle, Font, Border, Side,PatternFill

 

wb = Workbook()

ws = wb.active

 

highlight = NamedStyle(name="highlight")

highlight.font = Font(bold=True, size=20,color= "ff0100")

highlight.fill = PatternFill("solid", fgColor="DDDDDD")

bd = Side(style='thick', color="000000")  #边框颜色及粗细

highlight.border = Border(left=bd, top=bd, right=bd, bottom=bd) #边框 上下左右

 

print dir(ws["A1"])

ws["A1"].style =highlight #设置单元格样式

 

# Save the file

wb.save("e:\\test.xlsx")

 

 

30. 常用的样式和属性设置

# -*- coding: utf-8 -*-

from openpyxl import Workbook
from openpyxl.styles import Font
from openpyxl.styles import NamedStyle, Font, Border, Side,PatternFill
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font

wb = Workbook()
ws = wb.active

ft = Font(name=u'微软雅黑',
    size=11,
    bold=False,
    italic=False,
    vertAlign=None,
    underline='none',
    strike=False,
    color='FF000000')

fill = PatternFill(fill_type="solid",
    start_color='FFEEFFFF',
    end_color='FF001100')

#边框可以选择的值为:'hair', 'medium', 'dashDot', 'dotted', 'mediumDashDot', 'dashed', 'mediumDashed', 'mediumDashDotDot', 'dashDotDot', 'slantDashDot', 'double', 'thick', 'thin']
#diagonal 表示对角线
bd = Border(left=Side(border_style="thin",
              color='FF001000'),
    right=Side(border_style="thin",
               color='FF110000'),
    top=Side(border_style="thin",
             color='FF110000'),
    bottom=Side(border_style="thin",
                color='FF110000'),
    diagonal=Side(border_style=None,
                  color='FF000000'),
    diagonal_direction=0,
    outline=Side(border_style=None,
                 color='FF000000'),
    vertical=Side(border_style=None,
                  color='FF000000'),
    horizontal=Side(border_style=None,
                   color='FF110000')
                )

alignment=Alignment(horizontal='general',
        vertical='bottom',
        text_rotation=0,
        wrap_text=False,
        shrink_to_fit=False,
        indent=0)

number_format = 'General'

protection = Protection(locked=True,
            hidden=False)

ws["B5"].font = ft
ws["B5"].fill =fill
ws["B5"].border = bd
ws["B5"].alignment = alignment
ws["B5"].number_format = number_format

ws["B5"].value ="glory road"

# Save the file
wb.save("e:\\sample.xlsx")

 

转载于:https://www.cnblogs.com/qingqing-919/p/8337865.html


http://www.niftyadmin.cn/n/2574517.html

相关文章

2018.12.28-bzoj-2006-[NOI2010]超级钢琴

题目描述&#xff1a; 小Z是一个小有名气的钢琴家&#xff0c;最近C博士送给了小Z一架超级钢琴&#xff0c;小Z希望能够用这架钢琴创作出世界上最美妙的音乐。 这架超级钢琴可以弹奏出n个音符&#xff0c;编号为1至n。第i个音符的美妙度为Ai&#xff0c;其中Ai可正可负。 一个“…

【C++】 45_不同的继承方式

被忽略的细节&#xff1a;冒号 ( : ) 表示继承关系&#xff0c;Parent 表示被继承的类&#xff0c; public 的意义是什么呢&#xff1f; class Parent { };class Child : public Parent { }; 有趣的问题 是否可以将继承语句中的 public 换成 protected 或者 private &#xff1…

使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试

0、preliminary 环境搭建 Setup development environment Download the latest version of MRUnit jar from Apache website: https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/. For example if you are using the Hadoop version 1.0.…

Powershell管理系列(十四)Exchange 2013邮箱数量统计

-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包&#xff0c;QQ:185426445.电话18666943750管理Exchange的话&#xff0c;我们首先对自己管理的邮箱数量和分布情况有所了解。打开EAC我们确实很快就可以查到有多少邮箱数量&#xff0c;如果邮箱比较多的话…

还在找MCM模板?其实都内置了!用法见下

TeX live 2015及更新版本 和 MiKTeX &#xff08;以默认选项安装&#xff09;均内置了由Liam Huang维护的mcmthesis模板&#xff08;文档类&#xff09; 已被CTAN收录&#xff0c;可以说是全球认可的MCM模板了 所以&#xff0c;小伙伴们不要在找模板了&#xff0c;这个最好用 但…

LeetCode刷题 fIRST MISSING POSITIVE

Given an unsorted integer array,find missing postive integer. For example , Given [1,2,0]return 3, and [3,4,-1,1]return 2. Your algorithm should run in O(n) time and users constant space. 分析如下&#xff1a; 首先&#xff0c;要理解题解&#xff1a; 如果输入…

数据结构与算法--克鲁斯卡尔算法 最小生成树 Python详细实现克鲁斯卡尔算法 Python详细实现最小生成树

阅读目录基本概述克鲁斯卡尔算法图解克鲁斯卡尔算法分析Python代码实现补充知识点&#xff1a;如何获取int&#xff08;long&#xff09;和 float 的最值完整代码实现基本概述 先看一个应用场景&#xff1a; 克鲁斯卡尔算法介绍&#xff1a; 克鲁斯卡尔算法图解 还是以上面的…

应用 JD-Eclipse 插件实现 RFT 中 .class 文件的反向编译

概述 反编译是一个将目标代码转换成源代码的过程。而目标代码是一种用语言表示的代码 , 这种语言能通过实机或虚拟机直接执行。文本所要介绍的 JD-Eclipse 是一款反编译的开源软件&#xff0c;它是应用于 Eclipse 开发平台的插件&#xff0c;它可以帮助开发人员在调试程序的过程…