提交 c5b8faea 编辑于 作者: ParanoiaSYT's avatar ParanoiaSYT
浏览文件

python 操作excel over

上级 66a4d5d7
import openpyxl
wb=openpyxl.Workbook()
print(wb.sheetnames)
wb.remove(wb['Sheet'])
ws1=wb.create_sheet(title="小甲鱼",index=0)
ws2=wb.create_sheet(title="懒羊羊")
ws3=wb.create_sheet(title="皮鞋")
ws4=wb.create_sheet(title="飞燕")
# 个性化工作表标签栏
ws1.sheet_properties.tabColor="FF0000"
ws2.sheet_properties.tabColor="66CCFF"
ws3.sheet_properties.tabColor="8B008B"
ws4.sheet_properties.tabColor="0000FF"
# 调整行高和列宽
ws2.row_dimensions[2].height=100
ws2.column_dimensions['C'].width=50
# 注意虽然宽50,高100,但由于高、宽单位不同,所以显示不一样
# 合并拆分单元格
ws1.merge_cells("A1:C3")
ws1['A1']="I love fishc.com"
# 合并后只能赋值给左上角的位置(代表合并后整个单元格)
ws1.unmerge_cells("A1:C3")
# 拆分的范围要与合并的范围统一
# 拆分后留下的字在A1(左上角位置)
wb.save("FishCC.xlsx")
# 每次运行会覆盖保存
# 冻结窗口
# 打开现有的excel文件
wb=openpyxl.load_workbook("/Users/sunyuting/YuC-Study/爬虫项目/豆瓣top.xlsx")
ws=wb.active
ws.freeze_panes='C2'
# 表示C2位置左边和上面的单元格都被冻结,移动到别的单元格永远会显示
# ws.freeze_panes='A1'
ws.freeze_panes=None
# 解冻
wb.save('/Users/sunyuting/YuC-Study/爬虫项目/豆瓣top.xlsx')
import openpyxl
import openpyxl.styles
wb=openpyxl.Workbook()
ws=wb.active
b2=ws['B2']
b2.value="fishc"
bold_red_font=openpyxl.styles.Font(bold=True,color="FF0000")
b2.font=bold_red_font
b3=ws['B3']
b3.value="CCCC"
italic_strike_blue_16font=openpyxl.styles.Font(size=16,italic=True,strike=True,color="0000FF")
# italic倾斜,strike删除线
b3.font=italic_strike_blue_16font
# 填充单元格
from openpyxl.styles import PatternFill
# 纯色填充
yellow_fill=PatternFill(fill_type='solid',fgColor="FFFF00")
b2.fill=yellow_fill
from openpyxl.styles import GradientFill
# 渐进填充
red2green_fill=GradientFill(type='linear',stop=("FF0000","00FF00"))
# 填充方式线性,填充样式是一个元组形式(红-绿)
b3.fill=red2green_fill
# 绘制边框
from openpyxl.styles import Border,Side
thin_side=Side(border_style="thin",color="000000")
double_side=Side(border_style="double",color="0000FF")
# 绘制对角线
b2.border=Border(diagonal=thin_side,diagonalUp=True,diagonalDown=True)
# 绘制上下左右边框
b3.border=Border(left=double_side,top=double_side,right=double_side,bottom=double_side)
# 文本对齐
ws.merge_cells('A4:C5')
ws['A4']='I love fishc'
centr_alignment=openpyxl.styles.Alignment(horizontal="center",vertical="center")
# 水平垂直居中
ws['A4'].alignment=centr_alignment
# 命名样式
heighlight=openpyxl.styles.NamedStyle(name="heighlight")
heighlight.font=openpyxl.styles.Font(bold=True,size=20)
heighlight.alignment=openpyxl.styles.Alignment(horizontal="center",vertical="center")
# 命名了一个叫heighlight的样式模版
# 注册到工作簿里
wb.add_named_style(heighlight)
ws['A4'].style=heighlight
ws['D5']="我自横刀向天笑"
ws['D5'].style=heighlight
wb.save('样式.xlsx')
import openpyxl
import datetime
wb=openpyxl.Workbook()
......@@ -9,4 +10,40 @@ ws.append(['文本','数字'])
ws['A2']='520'
ws['B2']=520
wb.save('文本数字.xlsx')
\ No newline at end of file
ws['A3']=88.88
ws['A3'].number_format='#,###.00 $'
ws['B3']=datetime.datetime.today()
ws['B3'].number_format='yyyy-mm-dd'
# 设置数字格式
from openpyxl.styles.colors import RED,GREEN,BLUE,YELLOW
ws['A4'].number_format='[RED]+#,###.00;[GREEN]-#,###.00'
ws['A4']=999
ws['A5'].number_format='[RED]+#,###.00;[GREEN]-#,###.00'
ws['A5']=-999
# 通过分号为正负值做区分
ws['A6'].number_format='[RED][GREEN][BLUE][YELLOW]'
ws['A6']=0
ws['A7'].number_format='[RED];[GREEN];[BLUE];[YELLOW]'
ws['A7']='这是文本'
# 正值;负值;零值;文本
# 通过分号来设置格式
# 设置附加条件
ws['A8'].number_format='[=1]答对了;[=0]答错了'
ws['A8']=0
ws['A9'].number_format='[=1]答对了;[=0]答错了'
ws['A9']=1
ws['A10'].number_format='[=1]答对了;[=0]答错了'
ws['A10']=2
# 不符合条件的会乱码######
ws['A11'].number_format='[<60][RED]不及格;[>=60][BLUE]及格'
ws['A11']=59
wb.save('文本数字.xlsx')
import openpyxl
from openpyxl.utils import FORMULAE
import openpyxl.styles
print('SUM' in FORMULAE)
# 求和公式在函数里,导入成功
wb=openpyxl.load_workbook('test.xlsx')
print(wb.sheetnames)
ws=wb['Sheet']
for row in ws.iter_rows(min_col=2,min_row=2,max_col=5,max_row=5):
# row是每一行,row[3]就是每一行的第4列位置(注意是从min_col开始算的)
ws[row[3].coordinate]='=SUM(%s:%s)'%(row[0].coordinate,row[2].coordinate)
center_alignment=openpyxl.styles.Alignment(horizontal='center',vertical='center')
for row in ws.iter_rows(min_col=2,min_row=2,max_col=6,max_row=5):
ws[row[4].coordinate]='=IF(%s>250,"A","B")'%(row[3].coordinate)
# Excel里只认双引号解析
ws[row[4].coordinate].alignment=center_alignment
# LOOKUP函数查询(只能单行单列,切自动模糊匹配(没有确切值自动向下匹配)
ws['I2']='=LOOKUP(H2,D2:D5,A2:A5)'
ws['I2'].alignment=center_alignment
# 三个参数分别为查询值、查询范围、对应结果范围
# VLOOKUP函数
ws['J2']='=VLOOKUP(I2,A1:E5,5,FALSE)'
# 五个参数分别为查询值,查询范围,结果所在列(1开始数),是否进行模糊匹配
ws['J2'].alignment=center_alignment
wb.save('test.xlsx')
\ No newline at end of file
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册