import win32com.client as win32
# 打开Excel文件
excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(r'C:\Users\Thinkpad\Desktop\金溪水库资料整编2023110--V6.xlsx')
# 获取需要插入的Shape对象
worksheet = workbook.Worksheets('水平位移')
# 获取形状数量
shape_count = len(worksheet.Shapes)
#print(shape_count)
# 打开Word文件
word = win32.gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(r'C:\Users\Thinkpad\Desktop\1.docx')
i = shape_count
while i >= 1:
shape = worksheet.Shapes.Item(i) # 假设需要插入第一个Shape
# 在Word中插入Shape
selection = word.Selection
shape_range = selection.Range
shape.Copy()
shape_range.Paste()
i=i-1
# 保存Word文件并关闭Excel和Word
doc.Save()
doc.Close()
excel.Quit()
word.Quit()