第一步 安装openpyxl
pip install openpyxl
第二步 引用实例化
import openpyxl
path = f'{file_path}/tem_file/xxxx.xlsx'
wb = openpyxl.load_workbook(path)
sh = wb.active
第三步 xlsx 插入行(row)数
sh.insert_rows(9, 10)
第四步 对应的行列格插入数据
one_cell = sh.cell(row=7, column=1)
old_val = one_cell.value
val = "new_data"
sh.cell(7, 1, val)
第五步 单元格样式设置
from openpyxl.styles import Alignment, Border, Side
one_cell = sh.cell(row=7, column=1)
one_cell.border = Border(
left=Side(
border_style="thin",
color="FF000000"
),
right=Side(
border_style="thin",
color="FF000000"
),
top=Side(
border_style="thin",
color="FF000000"
),
bottom=Side(
border_style="thin",
color="FF000000"
)
)
one_cell.alignment = Alignment(horizontal='center', vertical='center')
...
第六步 文件保存
wb.save(path)