python插入word获取图表名称
原创
©著作权归作者所有:来自51CTO博客作者mb6418258ed13bf的原创作品,请联系作者获取转载授权,否则将追究法律责任
python把excel中所有的图表插入到word文件中
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()
python 获取excel表中所有图表名称
import win32com.client as win32
# 能把所有图片按原图象素导出来
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False # 可视化
excel.DisplayAlerts = False # 是否显示警告
workbook = excel.Workbooks.Open(r'C:\Users\Thinkpad\Desktop\金溪水库资料整编2023110--V6.xlsx')
file_names = [] #把图表名称存入列表
for sheet in workbook.Worksheets:
if sheet.Name == '水平位移':
for i, shape in enumerate(sheet.Shapes):
#print(sheet.Name, shape.Name)
file_names.append(shape.Name)
print(file_names)
excel.Quit()