请教一下在导出excel的时候按照图上的表头进行自定义?

请教一下在导出excel时候如果才能按照一下的表头进行自定义导出呢?
请教一下在导出excel的时候按照图上的表头进行自定义?

讨论数量: 5

pandas你值得拥有

1个月前 评论
kaoyu9 (楼主) 1个月前
Jason990420
import openpyxl
from openpyxl.styles import Alignment

data = [
    ['Name', 'John', 'Alice', 'Bob'],
    ['Age', 30, 25, 28],
    ['Location', 'New York', 'London', 'Paris']
]

wb = openpyxl.Workbook()
ws = wb['Sheet']

ws['A1'].value = "Table"
start_row = 1
end_row = 1
start_column = 1
end_column = len(data[0])

merge_range = f"{ws.cell(row=start_row, column=start_column).coordinate}:{ws.cell(row=end_row, column=end_column).coordinate}"
ws.merge_cells(merge_range)

for row in data:
    ws.append(row)

al = Alignment(horizontal='center', vertical='center')
for row in ws['A1':'D4']:
    for cell in row:
        cell.alignment = al

wb.save('example.xlsx')

file

1个月前 评论
kaoyu9 (楼主) 1个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!