当前位置:网站首页>Operating excel with openpyxl
Operating excel with openpyxl
2022-06-28 15:49:00 【Circle-C】
install
pip install openpyxl
Use
# Scope of access to
alphabet = '0ABCDEFGHIJKLMNOPQRSTUVWXYZ'
max_rows, max_cols = df.shape
area = f'A1:{
alphabet[max_cols]}{
max_rows + 1}'
fpath = os.path.join(path, f'Apollo contrast -{
service_name}.xlsx')
df.to_excel(fpath, index=False)
# Set the style
wb = load_workbook(fpath)
ws = wb[wb.sheetnames[0]] # Open the first one sheet
ws.column_dimensions['A'].width = 40.0 # Adjust columns A wide
env_col_names = [f'{
alphabet[i]}' for i in range(1, max_cols) if ws[f'{
alphabet[i]}1'].value in env_names]
env_col_names += [f'{
alphabet[i]}' for i in range(1, max_cols) if ' explain ' in (ws[f'{
alphabet[i]}1'].value or '') ]
for col_name in env_col_names: # Environmental Science key value 、 Description column width
ws.column_dimensions[col_name].width = 60.0
# Conditional format
red_fill = PatternFill(bgColor="FFC7CE")
dxf = DifferentialStyle(fill=red_fill)
compare_col_names = [f'{
alphabet[i]}' for i in range(1, max_cols) if ' Is it consistent ' in (ws[f'{
alphabet[i]}1'].value or '') ]
for col_name in compare_col_names:
r = Rule(type="expression", dxf=dxf, stopIfTrue=True)
r.formula = [f'${
col_name}1="FALSE"']
ws.conditional_formatting.add(f"{
col_name}1:{
col_name}{
max_rows + 10}", r)
# Screening
ws.auto_filter.ref = area
title_cols = ['key'] + env_names + [' Environment related ']
ws.auto_filter.add_filter_column(0, title_cols)
# Freeze the panes
ws.freeze_panes = 'A2'
black_fill = PatternFill(fgColor="222222")
title_font = Font(name='Microsoft YaHei UI',
size=12,
bold=True,
italic=False,
vertAlign=None,
underline='none',
strike=False,
color='000000')
font = Font(name='Microsoft YaHei UI',
size=10,
bold=False,
italic=False,
vertAlign=None,
underline='none',
strike=False,
color='000000')
# The background color of the column
for i in range(2, max_rows + 2):
ws[f'A{
i}'].fill=PatternFill(fill_type='solid',fgColor="dce6f1") # key 20% blue
ws[f'{
env_col_names[0]}{
i}'].fill=PatternFill(fill_type='solid',fgColor="ebf1de") # First environment name 20% green
ws[f'{
env_col_names[-1]}{
i}'].fill=PatternFill(fill_type='solid',fgColor="fde9d9") # explain 20% yellow
ws.cell(row=i, column=max_cols).fill=PatternFill(fill_type='solid',fgColor="e4dfec") # Environment related 20% purple
# Frame
# Define border styles
def my_border(t_border, b_border, l_border, r_border):
border = Border(top=Side(border_style=t_border, color=colors.BLACK),
bottom=Side(border_style=b_border, color=colors.BLACK),
left=Side(border_style=l_border, color=colors.BLACK),
right=Side(border_style=r_border, color=colors.BLACK))
return border
# Initializes the area border to all borders
def format_border(area):
for row in tuple(ws[area]):
for cell in row:
cell.border = my_border('thin', 'thin', 'thin', 'thin')
format_border(area)
边栏推荐
猜你喜欢
PostgreSQL实现按年、月、日、周、时、分、秒的分组统计

深度学习基础汇总

论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》

go-zero 微服务实战系列(七、请求量这么高该如何优化)

字节跳动数据平台技术揭秘:基于ClickHouse的复杂查询实现与优化

扩充C盘(将D盘的内存分给C盘)

See how the interface control devaxpress WinForms creates a virtual keyboard

智慧园区数智化供应链管理平台如何优化流程管理,驱动园区发展提速增质?

Application of mongodb in Tencent retail premium code

Cross cluster deployment of helm applications using karmada
随机推荐
Fleet | background Discovery issue 3: Status Management
Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)
开源大咖说 - Linus 与 Jim 对话中国开源
Oracle11g数据库使用expdp每周进行数据备份并上传到备份服务器
不要使用短路逻辑编写 stl sorter 多条件比较
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
机器学习之深度学习简介
Express template engine
部门新来了个字节25K出来的,让我见识到了什么是天花板
QT interface library
Visual Studio 2010 配置和使用Qt5.6.3
Installation and use of Jenkins
How can the digital intelligent supply chain management platform of the smart Park optimize process management and drive the development of the park to increase speed and quality?
10年测试经验,在35岁的生理年龄面前,一文不值
Flutter简单实现多语言国际化
Li Kou today's question -522 Longest special sequence
国债与定期存款哪个更安全 两者之间有何区别
Sample explanation of batch inserting data using MySQL bulkloader
IPDK — Overview
Privacy computing fat - offline prediction