python 写入数据到excel中

这篇博客介绍了如何利用Python的库将数据高效地写入Excel文件,为数据分析和报告生成提供便利。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python excel 写入数据到excel中

from openpyxl import Workbook
import os

from openpyxl.styles import Alignment
from openpyxl.styles import Border
from openpyxl.styles import Font
from openpyxl.styles import PatternFill
from openpyxl.styles import Side

file_name = '2.xlsx'
file_path = os.path.abspath(os.path.join(__file__, '../', file_name))
print(file_path)
wb = Workbook()
sheet = wb.active
sheet.title = '王飞'
index = 1

column_range = ['A', 'B', 'C']
column_widths = [10, 20, 30]
# 设置表格的宽度
for column_index, width in zip(column_range, column_widths):
    sheet.column_dimensions[column_index].width = width

# 绘制表格的颜色
# font = Font(name='微软雅黑', size=11, bold=True)
font = Font(name='微软雅黑', size=10)
fill = PatternFill(fill_type='solid', fgColor='FFFFFF')
alignment = Alignment(horizontal='general', vertical='center', wrap_text=True)
border = Border(left=Side(style='thin', color='000000'), right=Side(style='thin', color='000000'),
                    top=Side(style='thin', color='000000'), bottom=Side(style='thin', color='000000'))
# 填充内容
for i in range(1, 3):
    for j in range(1, 4):
        cell = sheet.cell(row=i, column=j)
        cell.value = i + j
        cell.alignment = alignment
        cell.font = font
        cell.border = border

# sheet.merge_cells(start_row=1, end_row=1, start_column=1, end_column=3) --------这个可以合并单元格
wb.save(file_path)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值