当前位置: 首页 > news >正文

Python生成Excel

import pandas as pd# 定义服务器配置清单工作表数据
data_server_config = [["序号", "资产编号", "设备种类", "品牌型号", "SN", "部门", "使用情况", "IP", "备注", "详情"],[1, "11 - 23456", "服务器", "HP xx G9", "xxx", "xx", "在用", "xxx", "", "CPU:2*E7 内存:128G"],[2, "11 - 56789", "服务器", "HP xx X5", "xxx", "xx", "在用", "xxx", "", "CPU:2*E7 内存:128G"],
]# 定义新平台工作表数据
data_new_platform = [["文件存储登录账号", "", "", "", "", "", ""],["服务项", "内网IP", "原默认端口", "修改后端口", "账户", "密码", ""],["", "", "", "", "", "", ""],
]# 定义旧平台工作表数据
data_old_platform = [["应用服务器登录账号", "", "", "", "", "", ""],["服务项", "内网IP", "原默认端口", "修改后端口", "账户", "密码", ""],["", "", "", "", "", "", ""],
]# 创建 DataFrame
df_server_config = pd.DataFrame(data_server_config[1:], columns=data_server_config[0])
df_new_platform = pd.DataFrame(data_new_platform[1:], columns=data_new_platform[0])
df_old_platform = pd.DataFrame(data_old_platform[1:], columns=data_old_platform[0])# 创建 Excel 文件并写入数据
output_path = '../../output/file/服务器清单.xlsx'
with pd.ExcelWriter(output_path, engine='openpyxl') as writer:df_server_config.to_excel(writer, sheet_name='服务器配置清单', index=False)df_new_platform.to_excel(writer, sheet_name='新平台', index=False)df_old_platform.to_excel(writer, sheet_name='旧平台', index=False)print("Excel文件已生成")

美化样式

import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter# 定义服务器配置清单工作表数据
data_server_config = [["序号", "资产编号", "设备种类", "品牌型号", "SN", "部门", "使用情况", "IP", "备注", "详情"],[1, "11 - 23456", "服务器", "HP xx G9", "xxx", "xx", "在用", "xxx", "", "CPU:2*E7 内存:128G"],[2, "11 - 56789", "服务器", "HP xx X5", "xxx", "xx", "在用", "xxx", "", "CPU:2*E7 内存:128G"],
]# 定义新平台工作表数据
data_new_platform = [["文件存储登录账号", "", "", "", "", "", ""],["服务项", "内网IP", "原默认端口", "修改后端口", "账户", "密码", ""],["", "", "", "", "", "", ""],
]# 定义旧平台工作表数据
data_old_platform = [["应用服务器登录账号", "", "", "", "", "", ""],["服务项", "内网IP", "原默认端口", "修改后端口", "账户", "密码", ""],["", "", "", "", "", "", ""],
]# 创建 DataFrame
df_server_config = pd.DataFrame(data_server_config[1:], columns=data_server_config[0])
df_new_platform = pd.DataFrame(data_new_platform[1:], columns=data_new_platform[0])
df_old_platform = pd.DataFrame(data_old_platform[1:], columns=data_old_platform[0])# 创建 Excel 文件并写入数据
output_path = '../../output/file/服务器清单.xlsx'
with pd.ExcelWriter(output_path, engine='openpyxl') as writer:df_server_config.to_excel(writer, sheet_name='服务器配置清单', index=False)df_new_platform.to_excel(writer, sheet_name='新平台', index=False)df_old_platform.to_excel(writer, sheet_name='旧平台', index=False)# 加载工作簿以应用样式
workbook = load_workbook(output_path)# 定义样式
header_font = Font(name='微软雅黑', size=12, bold=True, color='FFFFFF')
header_fill = PatternFill(start_color='4472C4', end_color='4472C4', fill_type='solid')
header_alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)subtitle_font = Font(name='微软雅黑', size=11, bold=True, color='FFFFFF')
subtitle_fill = PatternFill(start_color='70AD47', end_color='70AD47', fill_type='solid')
subtitle_alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)data_font = Font(name='微软雅黑', size=10)
data_alignment = Alignment(horizontal='left', vertical='center', wrap_text=True)border = Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),bottom=Side(style='thin')
)# 为每个工作表应用样式
for sheet_name in workbook.sheetnames:worksheet = workbook[sheet_name]# 自动调整列宽for column in worksheet.columns:max_length = 0column_letter = column[0].column_letterfor cell in column:try:if len(str(cell.value)) > max_length:max_length = len(str(cell.value))except:pass# 设置列宽(最大不超过50个字符)adjusted_width = min(max_length + 2, 50)worksheet.column_dimensions[column_letter].width = adjusted_width# 为第一行(标题行)应用特殊样式for cell in worksheet[1]:cell.font = header_fontcell.fill = header_fillcell.alignment = header_alignmentcell.border = border# 为数据行应用样式for row in worksheet.iter_rows(min_row=2):for cell in row:cell.font = data_fontcell.alignment = data_alignmentcell.border = border# 如果单元格内容为空,则设置背景色为浅灰色if cell.value == "" or cell.value is None:cell.fill = PatternFill(start_color='F2F2F2', end_color='F2F2F2', fill_type='solid')# 特殊处理新平台和旧平台工作表中的标题行if sheet_name in ['新平台', '旧平台']:for row_idx, row_data in enumerate(data_new_platform if sheet_name == '新平台' else data_old_platform):# 检查是否为标题行(第一个单元格包含特定文本且其他单元格为空)if (row_data[0] and("服务器" in row_data[0] or "登录账号" in row_data[0]) andall(cell == "" for cell in row_data[1:])):# 合并单元格(从A列到G列)start_col = 'A'end_col = get_column_letter(len(row_data))merge_range = f"{start_col}{row_idx + 1}:{end_col}{row_idx + 1}"worksheet.merge_cells(merge_range)# 应用标题样式cell = worksheet[f'A{row_idx + 1}']cell.font = subtitle_fontcell.fill = subtitle_fillcell.alignment = subtitle_alignmentcell.border = bordercell.value = row_data[0]  # 确保值正确显示# 保存美化后的Excel文件
workbook.save(output_path)
print("Excel文件已生成,包含样式设置")


文章转载自:

http://c6uplAPC.rykgh.cn
http://krwg3Sxm.rykgh.cn
http://nNF0r4Rh.rykgh.cn
http://i7hpkrZK.rykgh.cn
http://SoVmSFsp.rykgh.cn
http://LUeFsma6.rykgh.cn
http://Jf9Kj9Xd.rykgh.cn
http://Txmps5Eq.rykgh.cn
http://rUjcgn3M.rykgh.cn
http://uDQ001RO.rykgh.cn
http://zSoBHsHq.rykgh.cn
http://ysnV1LF0.rykgh.cn
http://4tBEZi9B.rykgh.cn
http://AxWcWuOn.rykgh.cn
http://YvwVMOtQ.rykgh.cn
http://LyN2VjlK.rykgh.cn
http://ksBXxxAv.rykgh.cn
http://Gfl2qH1z.rykgh.cn
http://QXqhnUpY.rykgh.cn
http://9UeFlcVc.rykgh.cn
http://aNd8Qz3w.rykgh.cn
http://xTzCnk5y.rykgh.cn
http://C1zmW0QK.rykgh.cn
http://M4XmVWSz.rykgh.cn
http://tbMcvW66.rykgh.cn
http://EZOMG0Rq.rykgh.cn
http://B5o64re0.rykgh.cn
http://QB7tNeN3.rykgh.cn
http://po4Y1cAX.rykgh.cn
http://dka2RQug.rykgh.cn
http://www.dtcms.com/a/363269.html

相关文章:

  • 点燃汽车电子与高端制造的“合规·高效·智能”引擎—— 全星研发项目管理软件系统APQP软件系统
  • CH01-1.2 Variable separable equation-Ordinary Differential Equation-by LiuChao
  • [架构之美]pdf压缩实战笔记(十五)
  • 【Unity Shader学习笔记】(一)计算机图形学概述
  • vue2 vue-property-decorator 库就类似于Java的注解库 vue class类编程
  • 阿里云和华为云Rocky LINUX 9.X镜像就绪及低端可用英伟达GPU
  • 力扣hot100:除自身以外数组的乘积(除法思路和左右前缀乘积)(238)
  • 静态ip软件哪个好用?资深用户的选择指南
  • Vite 插件 @vitejs/plugin-legacy 深度解析:旧浏览器兼容指南
  • 快速实现PLC之间的通信-基恩士
  • Spring Boot 全局字段处理最佳实践
  • 【程序员必备的Linux信号处理知识】
  • 【通用视觉框架】基于Python+OpenCV+PyQt5开发的视觉框架软件,全套源码,开箱即用
  • 变频器实习DAY41 单元测试介绍
  • % g++ *.cpp ...: fatal error: ‘opencv2/opencv.hpp‘ file not found 1
  • 趣味学RUST基础篇(错误处理)
  • Delphi 5 操作Word表格选区问题解析
  • 大数据毕业设计选题推荐-基于大数据的电脑硬件数据分析系统-Hadoop-Spark-数据可视化-BigData
  • 水电站电动机绝缘安全 “不掉线”!在线监测方案筑牢发电保障
  • ReactAgent接入MCP服务工具
  • 拷打字节面试官之-吃透c语言-哈希算法 如何在3面拷打字节cto 3万行算法源码带你吃透算法面试所有考题
  • C/C++条件编译:深入理解#ifndef/#endif守卫
  • 20.Linux进程信号(一)
  • C++拷贝语义和移动语义,左值引用与右值引用
  • 汉得H-AI飞码智能编码助手V1.2.4正式发布!
  • Turso数据库:用Rust重构的下一代SQLite——轻量级嵌入式数据库的未来选择
  • 三维重建——基础理论(四):三维重建基础与极几何原理(三维重建基础、单视图回忆、双目视觉、极几何、本质矩阵与基础矩阵、基础矩阵估计)
  • 虚实交互新突破:Three.js融合AR技术的孪生数据操控方法
  • 什么是 AWS 和 GCE ?
  • 解决Mac电脑连接蓝牙鼠标的延迟问题