Python获取excel内容及相关操作代码实例
Python没有自带openyxl,需要安装:pipinstallopenyxl
打开excel文档:openyxl.load_workbook(excel地址)-打开现有excel文件
openyxl.Workbook()-新建一个excel文件
返回一个工作博对象
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
print(type(wb))#
openpyxl.load_workbook()函数,传入一个存在的excel文件名称/excel文件名称+文件路径,返回一个workbook对象。
从workbook对象中获取工作表
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
#print(type(wb))#
#工作簿对象.sheetnames-获取当前工作簿中所有表的名字
#print(wb.sheetnames)['Sheet1','Sheet2','Sheet3']
#工作簿对象.active-获取当前活动表对应的Worksheet对象
#print(wb.active)
#工作簿对象[表名]-根据表名获取指定表对象
#print(wb["Sheet2"])
#表对象.title-获取表对象的表名
ws=wb["Sheet1"]
#print(ws.title)Sheet1
#表对象.max_row-获取表中最多有多少行
#print(ws.max_row)15
#表对象.max_column-获取表有多少列
print(ws.max_column)#3
从表中取得单元格
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["Sheet1"]
#表对象['列号行号']-获取指定列的指定行对应的单元格对象(单元格对象是Cell类的对象,列号是从A开始,行号是从1开始)
a=ws["A1"]
#print(a)#|
#单元格对象.value-获取单元格中的内容
print(a.value)
#单元格对象.row-获取行号(数字1开始)
print(a.row)
#单元格对象.column-获取列号(数字1开始)
print(a.column)
#单元格对象.coordinate-获取位置(包括行号和列号)
print(a.coordinate)
#表对象.iter_rows()-一行一行的取
row_s=ws.iter_rows()
forainrow_s:
foriina:
print(i.value)
#表对象.iter_cols()-列表一列的取
col_s=ws.iter_cols()
forcincol_s:
forjinc:
print(j.value) |
用字母来指定列时会出现列Z之后用两个字母代替,可以调用表的cell()方法,传入整数作为行数和列数,第一行或者第一列的整数是1,而不是0
表对象.cell(行号,列号)
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["Sheet1"]
#获取第二列的所有内容
max_row=ws.max_row
forrowinrange(1,max_row+1):
cell=ws.cell(row,2)
print(cell.value)
从表中取得列和行
取电子表格中一行、一列或一个矩形区域中的所有Cell对象
表对象[位置1:位置2]获取指定范围内的所有单元格
importopenpyxl
fromopenpyxl.utilsimportget_column_letter,column_index_from_string
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["Sheet1"]
max_row=ws.max_row
column=get_column_letter(max_row)
#获取第一列所有单元格对象
row2=ws["A1":f"{column}1"]
ss=[(cell.coordinate,cell.value)forcellsinrow2forcellincells]
print(ss)
importopenpyxl
fromopenpyxl.utilsimportget_column_letter,column_index_from_string
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["Sheet1"]
max_cols=ws.max_column
column=get_column_letter(max_cols)
#获取第一片区域所有单元格对象
row2=ws["A1":f"{column}3"]
ss=[(cell.coordinate,cell.value)forcellsinrow2forcellincells]
print(ss)
创建并保存Excel文档
openpyxl.Workbook()-创建空的Excel文件对应的工作薄对象
工作薄对象.save(文件路径)-保存文件
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["Sheet1"]
#修改sheet的名称
ws.title="hello_world"
wb.save("test.xlsx")
创建和删除sheet
工作薄对象.create_sheet(title,index)-在指定工作薄中的指定位置(默认是最后)创建指定名字的表,并返回表对象
工作薄对象.remove(表对象)-删除工作薄中的指定表
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
wb.create_sheet()
print(wb.sheetnames)
wb.create_sheet("test1")
print(wb.sheetnames)
wb.create_sheet("test2",index=0)
print(wb.sheetnames)
wb.remove(wb["test2"])
print(wb.sheetnames)
wb.save("test.xlsx")
将数据写入表格中
importopenpyxl
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["hello_world"]
#方式一
ws["A4"]="hello_world"
#方式二
ws.cell(4,5).value="hello_test"
wb.save("test.xlsx")
设置单元格样式
用表格展示数据的时候,有的时候需要对不同的数据以不同的风格进行展示从而达到分区或者强调的作用。
importopenpyxl
fromopenpyxl.stylesimportFont,PatternFill,Border,Side,Alignment
#1.打开工作薄
wb=openpyxl.load_workbook("test.xlsx")
ws=wb["hello_world"]
#2.设置单元格字体样式
"""
Font(
name=None,#字体名,可以用字体名字的字符串
strike=None,#删除线,True/False
color=None,#文字颜色
size=None,#字号
bold=None,#加粗,True/False
italic=None,#倾斜,Tue/False
underline=None#下划线,'singleAccounting','double','single','doubleAccounting'
)
"""
#1:创建字体对象
font1=Font(
size=20,
italic=True,
color="ff0000",
bold=True,
strike=True
)
#2:设置指定单元格的字体
#单元格对象.font=字体对象
ws["B2"].font=font1
#3:设置单元格填充样式
"""
PatternFill(
fill_type=None,#设置填充样式:'darkGrid','darkTrellis','darkHorizontal','darkGray','lightDown','lightGray','solid','lightGrid','gray125','lightHorizontal','lightTrellis','darkDown','mediumGray','gray0625','darkUp','darkVertical','lightVertical','lightUp'
start_color=None#设置填充颜色
)
"""
fill=PatternFill(
fill_type="solid",
start_color="ffff00"
)
ws["B2"].fill=fill
#设置单元格对齐样式
al=Alignment(
horizontal="right",#水平向方:center靠左:left靠右:right
vertical="top"#垂直方向:center,top,bottom
)
ws["B2"].alignment=al
#设置边框样式
#设置边对象(四个边可以是一样的也可以不同,如果不同就创建多个Side对象)
side=Side(border_style="thin",color="0000ff")
#设置边框对象(left、right、top、bottom表示的是边框的四个边,这儿四个边使用的是一个边对象)
db=Border(left=side,right=side,top=side,bottom=side)
ws["B2"].border=db
#设置单元格的宽度和高度
#设置指定列的宽度
ws.column_dimensions["A"].width=20
#设置指定行的高度
ws.row_dimensions[1].height=45
wb.save("test.xlsx")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。