#操作单元格
# -*- coding: utf-8 -*-
from openpyxl import Workbook
wb = Workbook()
ws1 = wb.create_sheet("Mysheet") #创建一个sheet
ws1["A1"]=123.11
ws1["B2"]="你好"
d = ws1.cell(row=4, column=2, value=10)
print ws1["A1"].value
print ws1["B2"].value
print d.value
# Save the file
wb.save("e:\\sample.xlsx")
#操作批量单元格
#无论ws.rows还是ws.iter_rows都是一个对象
#除上述两个对象外 单行,单列都是一个元祖,多行多列是二维元祖
# -*- coding: utf-8 -*-
from openpyxl import Workbook
wb = Workbook()
ws1 = wb.create_sheet("Mysheet") #创建一个sheet
ws1["A1"]=1
ws1["A2"]=2
ws1["A3"]=3
ws1["B1"]=4
ws1["B2"]=5
ws1["B3"]=6
ws1["C1"]=7
ws1["C2"]=8
ws1["C3"]=9
#操作单列
print ws1["A"]
for cell in ws1["A"]:
print cell.value
#操作多列,获取每一个值
print ws1["A:C"]
for column in ws1["A:C"]:
for cell in column:
print cell.value
#操作多行
row_range = ws1[1:3]
print row_range
for row in row_range:
for cell in row:
print cell.value
print "*"*50
for row in ws1.iter_rows(min_row=1, min_col=1, max_col=3, max_row=3):
for cell in row:
print cell.value
#获取所有行
print ws1.rows
for row in ws1.rows:
print row
print "*"*50
#获取所有列
print ws1.columns
for col in ws1.columns:
print col
wb.save("e:\\sample.xlsx")
#使用百分数
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
wb.guess_types = True
ws=wb.active
ws["D1"]="12%"
print ws["D1"].value
# Save the file
wb.save("e:\\sample.xlsx")
#结果会打印小数
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
wb.guess_types = False
ws=wb.active
ws["D1"]="12%"
print ws["D1"].value
wb.save("e:\\sample.xlsx")
#结果会打印百分数
#获取所有行对象
#coding=utf-8
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
ws=wb.active
rows=[]
for row in ws.iter_rows():
rows.append(row)
print rows #所有行
print rows[0] #获取第一行
print rows[0][0] #获取第一行第一列的单元格对象
print rows[0][0].value #获取第一行第一列的单元格对象的值
print rows[len(rows)-1] #获取最后行 print rows[-1]
print rows[len(rows)-1][len(rows[0])-1] #获取第后一行和最后一列的单元格对象
print rows[len(rows)-1][len(rows[0])-1].value #获取第后一行和最后一列的单元格对象的值
#获取所有的列对象
#coding=utf-8
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
ws=wb.active
cols=[]
cols = []
for col in ws.iter_cols():
cols.append(col)
print cols #所有列
print cols[0] #获取第一列
print cols[0][0] #获取第一列的第一行的单元格对象
print cols[0][0].value #获取第一列的第一行的值
print "*"*30
print cols[len(cols)-1] #获取最后一列
print cols[len(cols)-1][len(cols[0])-1] #获取最后一列的最后一行的单元格对象
print cols[len(cols)-1][len(cols[0])-1].value #获取最后一列的最后一行的单元格对象的值
#操作已经存在的文件
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
wb.guess_types = True #猜测格式类型
ws=wb.active
ws["D1"]="12%"
print ws["D1"].value
# Save the file
wb.save("e:\\sample.xlsx")
#注意如果原文件有一些图片或者图标,则保存的时候可能会导致图片丢失
#单元格类型
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
import datetime
wb = load_workbook('e:\\sample.xlsx')
ws=wb.active
wb.guess_types = True
ws["A1"]=datetime.datetime(2010, 7, 21)
print ws["A1"].number_format
ws["A2"]="12%"
print ws["A2"].number_format
ws["A3"]= 1.1
print ws["A4"].number_format
ws["A4"]= "中国"
print ws["A5"].number_format
# Save the file
wb.save("e:\\sample.xlsx")
执行结果:
yyyy-mm-dd h:mm:ss
0%
General
General
#如果是常规,显示general,如果是数字,显示'0.00_ ',如果是百分数显示0%
数字需要在Excel中设置数字类型,直接写入的数字是常规类型
#使用公式
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
ws1=wb.active
ws1["A1"]=1
ws1["A2"]=2
ws1["A3"]=3
ws1["A4"] = "=SUM(1, 1)"
ws1["A5"] = "=SUM(A1:A3)"
print ws1["A4"].value #打印的是公式内容,不是公式计算后的值,程序无法取到计算后的值
print ws1["A5"].value #打印的是公式内容,不是公式计算后的值,程序无法取到计算后的值
# Save the file
wb.save("e:\\sample.xlsx")
#合并单元格
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('e:\\sample.xlsx')
ws1=wb.active
ws.merge_cells('A2:D2')
ws.unmerge_cells('A2:D2') #合并后的单元格,脚本单独执行拆分操作会报错,需要重新执行合并操作再拆分
# or equivalently
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)
# Save the file
wb.save("e:\\sample.xlsx")
#插入一个图片(需要先安装Pilow,安全文件是:PIL-fork-1.1.7.win-amd64-py2.7.exe)
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
wb = load_workbook('e:\\sample.xlsx')
ws1=wb.active
img = Image('e:\\1.png')
ws1.add_image(img, 'A1')
# Save the file
wb.save("e:\\sample.xlsx")
#隐藏单元格
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
wb = load_workbook('e:\\sample.xlsx')
ws1=wb.active
ws1.column_dimensions.group('A', 'D', hidden=True) #隐藏a到d列范围内的列
#ws1.row_dimensions 无group方法
# Save the file
wb.save("e:\\sample.xlsx")
12、 画一个柱状图
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series
wb = load_workbook('e:\\sample.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)
ws.add_chart(chart, "E15")
# Save the file
wb.save("e:\\sample.xlsx")
#画一个饼图
12、 画一个柱状图
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series
wb = load_workbook('e:\\sample.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)
ws.add_chart(chart, "E15")
# Save the file
wb.save("e:\\sample.xlsx")
#设定一个表格区域并设定表格的格式
12、 画一个柱状图
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series
wb = load_workbook('e:\\sample.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)
ws.add_chart(chart, "E15")
# Save the file
wb.save("e:\\sample.xlsx")
#给单元格设定字体颜色
12、 画一个柱状图
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series
wb = load_workbook('e:\\sample.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)
ws.add_chart(chart, "E15")
# Save the file
wb.save("e:\\sample.xlsx")
#设定字体和大小
# -*- 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']
a1.value = "abc"
from openpyxl.styles import Font
from copy import copy
ft1 = Font(name=u'宋体', size=14)
ft2 = copy(ft1) #复制字体对象
ft2.name = "Tahoma"
print ft1.name
print ft2.name
print ft2.size # copied from the
a1.font = ft1
# Save the file
wb.save("e:\\sample.xlsx")
#设定行和列的字体
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
col = ws.column_dimensions['A']
col.font = Font(bold=True) #将A列设定为粗体
row = ws.row_dimensions[1]
row.font = Font(underline="single") #将第一行设定为下划线格式
# Save the file
wb.save("e:\\sample.xlsx")
#设定单元格的边框、字体、颜色、大小和边框背景色
# -*- 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:\\sample.xlsx")
#常用的样式和属性设置
# -*- 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:\\sample.xlsx")
Python 操作excel-4
最新推荐文章于 2024-06-21 10:34:53 发布