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)