用python把excel表格插入word代码

import openpyxl
from docx import Document
from docx.shared import Inches

# 打开Excel表格并读取数据
wb = openpyxl.load_workbook('table.xlsx')
ws = wb.active

# 创建Word文档并添加表格
document = Document()
document.add_heading('Excel表格', level=0)
table = document.add_table(rows=ws.max_row, cols=ws.max_column)

# 将Excel表格数据填充到Word表格中
for i in range(ws.max_row):
for j in range(ws.max_column):
cell = table.cell(i, j)
cell.text = str(ws.cell(row=i+1, column=j+1).value)

# 将Word文档保存到文件中
document.save('table.docx')

你可能感兴趣的:(word,excel)