前言:openpyxl是一个用于读/写excel 2010 xlsx/xlsm/xltx/xltm文件的python库,所有对phpexcel团队的赞誉都是因为openpyxl最初是基于phpexcel的。以下从Excel格式处理,单元格数值修改等方面详解openpyxl的操作功能。
1.处理表格
from openpyxl import load_workbook
import datetime
wb = load_workbook(r'D:\Pyfiles\xx公司指标体系.xlsx')
2.sheet设置
#创建sheet
ws1=wb.create_sheet("mysheet")
#设定sheet的名字
ws1.title='newtitle'
#设定sheet的标签的背景颜色
ws1.sheet_properties.tabColor='1072BA'
#获取某个sheet对象
print(wb.get_sheet_by_name("newtitle"))
print(wb["newtitle"])
#遍历sheet名字
print(wb.sheetnames)
for sheetname in wb.sheetnames:
print(sheetname)
for sheet in wb:
print(sheet.title)
#复制一个sheet
wb['newtitle']['A1']='123'
source=wb["newtitle"]
target=wb.copy_worksheet(source)
#操作单元格
ws = wb.active
#调整列宽
ws.column_dimensions['A'].width = 10
#调整行高
ws.row_dimensions[1].height = 13.5
d=ws.cell(row=4,column=2)
#d=ws.cell(row=4,column=2,value=10)//更改该单元格的值
print(d.value)
#操作单列
print(ws['B'])
for cell in ws['B']:
print(cell.value)
#操作多列
print(ws["A:C"])
for column in ws["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
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 ws1.columns
for col in ws1.columns:
print col
#获取所有的行对象
rows1=[]
for row in ws.iter_rows():
rows1.append(row)
print(rows1[2][3].value)
#获取所有的列对象
col1=[]
for col in ws.iter_cols():
col1.append(col)
print(col1[2][2].value)
#单元格格式
ws['B2']=datetime.datetime(2021,7,21)
print(ws['B2'].number_format)#yyyy-mm-dd h:mm:ss
ws['B3']='12%'
print(ws['B3'].number_format)#General
#使用公式
ws["A1"]=1
ws["A2"]=2
ws["A3"]=3
ws["A4"] = "=SUM(1, 1)"
ws["A5"] = "=SUM(A1:A3)"
print (ws["A4"].value ) #打印的是公式内容,不是公式计算后的值,程序无法取到计算后的值
print (ws["A5"].value) #=SUM(A1:A3)
#合并单元格
ws.merge_cells('B2:B4')
ws.merge_cells(start_row=2,start_column=1,end_row=2,end_column=4)
#拆分单元格
ws.unmerge_cells('B2:B4')
ws.unmerge_cells(start_row=2,start_column=1,end_row=2,end_column=4)
#隐藏单元格
ws.column_dimensions.group('A','D',hidden=False)
ws.row_dimensions.group(5,7,hidden=False)
#设定字体格式Font()
ws=wb.active
import openpyxl
from openpyxl.styles import fills,colors,NamedStyle,Font,Side,Border,PatternFill,Alignment,Protection
for irow,row in enumerate(ws.rows,start=1): # 加粗 斜体 下划线
font=Font('微软雅黑',size=11,color=colors.BLACK,bold=False,italic=True,underline='double')
for cell in row:
cell.font=font
if irow%3==0:
cell.fill=fills.GradientFill(stop=['FF0000', '0000FF'])#填充渐变
from openpyxl.styles import fills,colors,NamedStyle,Font,Side,Border,PatternFill,Alignment,Protection
3.格式处理示例
#字体
ft = Font(name=u'微软雅黑',
size=11,
bold=True,
italic=True,#斜体
vertAlign='baseline',#上下标'subscript','baseline'='none,'superscript'
underline='single',#下划线'singleAccounting', 'double', 'single', 'doubleAccounting'
strike=False ,#删除线
color='00FF00')
fill = PatternFill(fill_type="solid",
start_color='FFFFFF',#单元格填充色
end_color='FFFFFF')
#边框 可以选择的值为:'hair', 'medium', 'dashDot', 'dotted', 'mediumDashDot', 'dashed', 'mediumDashed', 'mediumDashDotDot', 'dashDotDot', 'slantDashDot', 'double', 'thick', 'thin']
bd = Border(left=Side(border_style="thin",
color='0000FF'),
right=Side(border_style="double",
color='5C3317'),
top=Side(border_style="thin",
color='FF110000'),
bottom=Side(border_style="hair",
color='238E23'),
diagonal=Side(border_style='dashed',#对角线
color='3299CC'),
diagonal_direction=1,
outline=Side(border_style='slantDashDot',#外边框
color='BC1717'),
vertical=Side(border_style='medium',#竖直线
color=colors.BLACK),
horizontal=Side(border_style='dotted',#水平线
color=colors.WHITE)
)
#对齐方式
alignment=Alignment(horizontal='center',#水平'center', 'centerContinuous', 'justify', 'fill', 'general', 'distributed', 'left', 'right'
vertical='top',#垂直'distributed', 'bottom', 'top', 'center', 'justify'
text_rotation=0,#旋转角度0~180
wrap_text=False,#文字换行
shrink_to_fit=True,#自适应宽度,改变文字大小,上一项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 ="123"
wb.save(r'D:\Pythontest\reptile\豆瓣排名250.xlsx')
颜色编码参考表:https://www.sioe.cn/yingyong/yanse-rgb-16/
4.填充、边框、根据首行内容自动调整列宽
import openpyxl
from openpyxl.styles import Font,PatternFill,Side,Alignment,Border
def reset_color(filename):
wb=openpyxl.load_workbook(filename)
fill=PatternFill( #设置填充样式
fill_type='solid',
start_color='99ccff')
border=Border(top=Side(border_style='thin',color='000000'), #设置边框样式
bottom=Side(border_style='thin',color='000000'),
left=Side(border_style='thin',color='000000'),
right=Side(border_style='thin',color='000000'))
for i in wb.sheetnames:
ws=wb[i]
ws.sheet_view.showGridLines=False #隐藏默认网线
for c in range(1,ws.max_column+1):
for r in range(1,ws.max_row+1): #添加边框
bordercell=ws.cell(r,c)
bordercell.border=border
for end in range(1,ws.max_column+1):
fillcell=ws.cell(1,end)
fillcell.fill=fill #填充首行
wb.save(filename)
def reset_col(filename):
wb=load_workbook(filename)
for sheet in wb.sheetnames:
ws=wb[sheet]
df=pd.read_excel(filename,sheet)
for col in df.columns:
index=list(df.columns).index(col)
letter=get_column_letter(index+1)
collen=len(str(col).encode()) #读取首行(标题行)宽度
print(collen)
ws.column_dimensions[letter].width=collen+4 #设置列宽为当前列标题宽度+4
wb.save(filename)
filename=r'{}\2.xlsx'.format(path)
reset_color(filename)
reset_col(filename)
5.设置行高列宽
import openpyxl
wb=openpyxl.Workbook()
sheet=wb.active
# 设置行高
sheet['A1']='行高被设置为 100'
sheet.row_dimensions[1].height=100
# 设置列宽
sheet['B2']='列宽被设置为 50'
sheet.column_dimensions['B'].width=50
wb.save('dimensions.xlsx')
#Excel默认行高为13.5,列宽为8.38
注意:通过脚本对表格进行操作的时候,不要用其他软件打开表格,否则可能会造成脚本运行失败
1、向某个格子中写入内容并保存
方式一:相当于直接修改源文件中的单元格
from openpyxl import load_workbook
workbook = load_workbook(filename='../excelDemo/testCase.xlsx')
sheet = workbook.active
print(sheet)
# 方式一:相当于直接修改源文件
sheet["C2"] = "无效"
workbook.save(filename='../excelDemo/testCase.xlsx')
方式二:修改单元格内容后另存文件
from openpyxl import load_workbook
workbook = load_workbook(filename='../excelDemo/testCase.xlsx')
sheet = workbook.active
print(sheet)
# 方式二:将“C4”单元格的数据改为了“失效”,并另存为了“test.xlsx”文件(路径为当前py程序下)
cell = sheet["C4"]
cell.value = "失效"
workbook.save(filename = "test.xlsx")
2、向表格中插入行数据 .append()
.append()方式:会在表格已有的数据后面,增添这些数(按行插入);
这个操作很有用,爬虫得到的数据,可以使用该方式保存成 Excel 文件
from openpyxl import load_workbook
workbook = load_workbook(filename='./test1.xlsx')
sheet = workbook.active
# print(sheet)
data = [
["机审规则","不同用户类型跑不同publine","主"],
["机审规则","不同用户类型跑不同publine","低"],
["机审规则","不同用户类型跑不同publine","中"],
["机审规则","不同用户类型跑不同publine","中"],
]
for row in data:
sheet.append(row)
# print(row)
workbook.save(filename='./test2.xlsx')
3、在 python 中使用 excel 函数公式
python 究竟支持写哪些“excel 函数公式”呢?我们可以使用如下操作查看一下
from openpyxl import load_workbook
from openpyxl.utils import FORMULAE
print(FORMULAE)
计算平均值
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
# print(sheet)
sheet["k1"] = "平均消费"
for i in range(2,5):
sheet[f"k{i}"] = f'=AVERAGE(B{i}:J{i})'
workbook.save(filename='./cash.xlsx')
4、插入空行和空列 .insert_cols()和.insert_rows()
.insert_cols(idx=数字编号, amount=要插入的列数),插入的位置是在 idx 列数的左
侧插入;
.insert_rows(idx=数字编号, amount=要插入的行数),插入的行数是在 idx 行数的下
方插入;
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
# print(sheet)
sheet.insert_cols(idx=2,amount=1)
# 在第二列插入一列空列
sheet.insert_rows(idx=3,amount=2)
# 在第三行插入两行空行
workbook.save(filename='./cash.xlsx')
效果如下,在第二列插入一列空列,在第三行插入两行空行。
注意:
通过函数对表格进行操作时,表格不能用其他软件进行打开,否则该脚本会运行失败
插入空行和空列,会对之前的使用 excel 函数公式产生影响,例如饮料喝肉类两行的平均消费的值就发生了改变,因为计算平均值的时候,是根据所限定范围内的单元格进行操作的,插入空行和空列后,指定的单元格的内容已经发生了改变
5、删除行和列 .delete_rows()和.delete_cols()
.delete_rows(idx=数字编号, amount=要删除的行数)
.delete_cols(idx=数字编号, amount=要删除的列数)
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
# print(sheet)
sheet.delete_cols(idx=2,amount=1)
# 删除第二列
sheet.delete_rows(idx=3,amount=2)
# 从第三行开始删除两行
workbook.save(filename='./cash.xlsx')
效果如下,删除第二列,从第三行开始删除两行,表格又回复原样了
6、移动格子 .move_range()
.move_range("数据区域",rows=,cols=):正整数为向下或向右、负整数为向左或向上
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
# print(sheet)
sheet.move_range("D2:G5",rows=2,cols=1)
# 将"D2:G5"这块区域向下平移2行,向右平移一列
workbook.save(filename='./cash.xlsx')
效果如下
7、创建新的 sheet 表格 .create_sheet()
.create_sheet("新的 sheet 名"):创建一个新的 sheet 表
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
# print(sheet)
workbook.create_sheet("newSheet")
print(workbook.sheetnames)
# ['Sheet1', 'newSheet'] 现在存在两个sheet表,其中newSheet是新建的
workbook.save(filename='./cash.xlsx')
效果如下
8、删除某个 sheet 表 .remove()
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
print(workbook.sheetnames)
# ['Sheet1', 'newSheet'] 目前有两个sheet表
# 这个操作相当于激活的这个要删除的 sheet 表,激活状态下,才可以操作;
remove_sheet = workbook['newSheet']
workbook.remove(remove_sheet)
print(workbook.sheetnames)
# ['Sheet1'] newSheet表已被删除,现在只有一张表
workbook.save(filename='./cash.xlsx')
9、复制一个 sheet 表 .copy_worksheet()
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
print(workbook.sheetnames)
# ['Sheet1']
# 这个操作相当于激活的这个要操作的 sheet 表,激活状态下,才可以操作;
copy_sheet = workbook['Sheet1']
workbook.copy_worksheet(copy_sheet)
print(workbook.sheetnames)
# ['Sheet1', 'Sheet1 Copy']
workbook.save(filename='./cash.xlsx')
效果如下
当然,你还可以再对这个复制出来的表再进行一次复制操作
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
print(workbook.sheetnames)
# ['Sheet1', 'Sheet1 Copy']
# 这个操作相当于激活的这个要操作的 sheet 表,激活状态下,才可以操作;
copy_sheet = workbook['Sheet1 Copy']
workbook.copy_worksheet(copy_sheet)
print(workbook.sheetnames)
# ['Sheet1', 'Sheet1 Copy', 'Sheet1 Copy Copy']
workbook.save(filename='./cash.xlsx')
10、修改 sheet 表的名称 sheet.title
.title = "新的 sheet 表名"
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
print(workbook.sheetnames)
# ['Sheet1', 'Sheet1 Copy', 'Sheet1 Copy Copy']
sheet.title = "sheetUpdate"
print(workbook.sheetnames)
# ['sheetUpdate', 'Sheet1 Copy', 'Sheet1 Copy Copy'],第一张 sheet 表的表名已改变
# 若excel表中存在多个 sheet 表,默认修改第一张 sheet 表
# 也可以指定 sheet 表更改表名
updateSheet = workbook['Sheet1 Copy Copy']
updateSheet.title = "sheetUpdate2"
print(workbook.sheetnames)
# ['sheetUpdate', 'Sheet1 Copy', 'sheetUpdate2']
workbook.save(filename='./cash.xlsx')
11、创建新的 excel 表格文件
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
# sheet 表名为"表格 1"
sheet.title = "表格 1"
# excel 表名为 “新建的 excel 表格”
workbook.save(filename = "新建的 excel 表格")
12、冻结窗口 sheet.freeze_panes
.freeze_panes = "单元格"
from openpyxl import load_workbook
workbook = load_workbook(filename='./cash.xlsx')
sheet = workbook.active
print(sheet)
# 冻结 "C3"单元格
sheet.freeze_panes = "C3"
workbook.save(filename='./cash.xlsx')
效果如下:第一二行固定在表格顶部,第一二列固定在左边,其余可拖动滚动条进行滑动查看
13、给表格添加“筛选器” sheet.auto_filter.ref
.auto_filter.ref = sheet.dimension 给所有字段添加筛选器;
.auto_filter.ref = "A1" 给 A1 这个格子添加“筛选器”,就是给第一列添加“筛选器”
from openpyxl import load_workbook
workbook = load_workbook(filename='./test2.xlsx')
sheet = workbook.active
# print(sheet)
sheet.auto_filter.ref = "C1"
workbook.save(filename='./test2.xlsx')
效果如下
资料参考:
https://blog.csdn.net/qq_44614026/article/details/108213906
https://blog.csdn.net/weixin_47536499/article/details/115417784
https://blog.csdn.net/qq_42819930/article/details/89463202
感谢各位博主的知识分享,本文在此基础上进行了进一步详细的归纳。
让我们一起,为梦想窒息。