当前位置:网站首页>openpyxl 单元格合并
openpyxl 单元格合并
2022-08-02 08:15:00 【皮卡大人】
需求:生成一个excel文件,但是表头有许多内容需要合并单元格,那个表头的内容又相当多。于是就自己写了一个方法,方便写入表头。
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.reader.excel import load_workbook
from openpyxl.styles import Alignment
from zipfile import ZipFile, ZIP_DEFLATED
def write_hear(ws, hear, start_row, end_row, i):
""" 写入表头 :param ws: worksheet :param hear: 表头 :param start_row: 开始-行 :param end_row: 结束-行 :param i: 列 :return: 无 """
alignment_center = Alignment(horizontal='center', vertical='center')
for item in hear:
if width := item.get('width'):
ws.merge_cells(start_row=start_row, start_column=i, end_row=start_row, end_column=i + width - 1)
ws.cell(row=start_row, column=i, value=item['label']).alignment = alignment_center
write_hear(ws, item['children'], start_row+1, end_row, i)
i = i + width
else:
ws.merge_cells(start_row=start_row, start_column=i, end_row=end_row, end_column=i)
ws.cell(row=start_row, column=i, value=item['label']).alignment = alignment_center
i = i + 1
def write_data(ws, start_row, records):
""" 数据写入表格 :param ws: :param start_row: :param records: 内容数据,二维数组[][] :return: 无 """
i = start_row + 1
for record in records:
for x in range(1, len(record) + 1):
ws.cell(i, x, record[x-1])
i += 1
def write_excel_new(hear, hear_row, records, sheet_name='Sheet1', save_excel_name='save.xlsx'):
""" 生成xlsx文件 :param hear: 表头内容 :param hear_row: 表头占的行数 :param records: 内容 :param sheet_name: 标题 :param save_excel_name: 文件名 :return: 无 """
wb = Workbook()
archive = ZipFile(save_excel_name, 'w', ZIP_DEFLATED)
ew = ExcelWriter(workbook=wb, archive=archive)
ws = wb.worksheets[0]
ws.title = sheet_name
ws.column_dimensions.width = 20
write_hear(ws, hear, 1, hear_row, 1)
# write_data(ws, hear_row, records)
ew.save()
hear_ = [
{
'label': '所属班组'},
{
'label': '电压等级(kV)'},
{
'label': '线路名称'},
{
'label': '出线站'},
{
'label': '进线站'},
{
'label': '调度编号'},
{
'label': '色标'},
{
'label': '线路长度(km)', 'width': 5,
'children': [
{
'label': '单回路'},
{
'label': '双回路'},
{
'label': '三回路'},
{
'label': '四回路及以上'},
{
'label': '总长度'}
]
},
{
'label': '杆塔数量(基)', 'width': 7,
'children': [
{
'label': '水泥杆'},
{
'label': '铁塔', 'width': 3,
'children': [
{
'label': '钢管杆'},
{
'label': '角钢塔'},
{
'label': '钢管组合塔'},
]
},
{
'label': '直线'},
{
'label': '耐张'},
{
'label': '总数'},
]
},
{
'label': '杆塔物理数量统计(基)', 'width': 4,
'children': [
{
'label': '单回路'},
{
'label': '双回路'},
{
'label': '三回路'},
{
'label': '四回路及以上'}
]
},
{
'label': '导地线型号', 'width': 3,
'children': [
{
'label': '导线型号'},
{
'label': '地线型号', 'width': 2,
'children': [
{
'label': '左地线'},
{
'label': '右地线'},
]
}
]
},
{
'label': '投产日期'},
{
'label': '备注'},
{
'label': '有无摄像头'}
]
if __name__ == '__main__':
write_excel_new(hear_, 3, None)
hear_:是我定义的一个表头字典。
write_hear:把**hear_**字典生成表头,至于行宽什么的看着设置。
write_data:写入数据,在上面的代码里,我注释掉了,所以传Node。
要是还有什么需求,可以找官网第三方库openpyxl
的官网
边栏推荐
- In a recent build figure SLAM, and locate the progress
- 【特别提醒】订阅此专栏的用户请先阅读本文再决定是否需要购买此专栏
- Biotin-EDA|CAS:111790-37-5| Ethylenediamine biotin
- 如何做好项目管理
- C语言基础_结构体
- Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
- pycharm的基本使用教程(1)
- postman下载安装汉化及使用
- 文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
- 高仿【华为消费者业务官网】和精彩动画剖析:练习在低代码平台中嵌入JS代码
猜你喜欢
Biotin hydrazide HCl|CAS:66640-86-6|Biotin-hydrazide hydrochloride
USACO美国信息学奥赛竞赛12月份开赛,中国学生备赛指南
Database Plus 的云上之旅:SphereEx 正式开源 ShardingSphere on Cloud 解决方案
etcd implements large-scale service governance application combat
TiFlash 存储层概览
图扑软件数字孪生油气管道站,搭建油气运输管控平台
三维体尺测量
用C写小游戏(三子棋)
Biotin-LC-Hydrazide|CAS:109276-34-8|生物素-LC-酰肼
Biotin hydrazide HCl|CAS:66640-86-6|生物素-酰肼盐酸盐
随机推荐
(Note)阿克西斯ACASIS DT-3608双盘位硬盘阵列盒RAID设置
52. [Bool type input any non-0 value is not 1 version reason]
[ansible] playbook explains the execution steps in combination with the project
Seleniu screenshots code and assign name to the picture
prometheus监控mysql_galera集群
Biotin-LC-Hydrazide|CAS:109276-34-8|生物素-LC-酰肼
爬虫——爬虫初识、requests模块
Pycharm (1) the basic use of tutorial
R language plotly visualization: use the plotly visualization model to predict the true positive rate (True positive) TPR and false positive rate (False positive) FPR curve under different thresholds
Three types of [OC learning notes] Block
王学岗-编译出运行的文件
shell中计算命令详解(expr、(())、 $[]、let、bc )
etcd implements large-scale service governance application combat
C语言基础_结构体
Biotin hydrazide HCl|CAS:66640-86-6|生物素-酰肼盐酸盐
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
积分商城商品供应商选择的三个要求
如何做好项目管理
Biotin-EDA|CAS:111790-37-5| 乙二胺生物素
High imitation [Huawei consumer business official website] and wonderful animation analysis: practice embedding JS code in low-code platform