You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# pip install pywin32
# https://blog.csdn.net/weixin_42927998/article/details/115086797
import win32com
from win32com . client import Dispatch
docApp = win32com . client . Dispatch ( ' Word.Application ' )
# 是不是打Word显示
docApp . Visible = False
docApp . DisplayAlerts = 0
# doc = docApp.Documents.Open('c:/1.docx')
doc = docApp . Documents . Open ( ' c:/昭通市人口变化及其对教育的影响20240416.docx ' )
'''
Type
1: 柱形图( Column)
2: 折线图( Line)
3: 饼图( Pie)
51: 堆叠柱形图( Stacked Column)
52: 堆叠线图( Stacked Line)
53: 堆叠区域图( Stacked Area)
55: 雷达图( Radar)
65: 树状图( Treemap)
73: 旭日图( Sunburst)
77: 水桶图( Funnel)
109: 散点图( Scatter)
183: 气泡图( Bubble)
'''
# 遍历文档中的所有内嵌形状
idx = 1
for inline_shape in doc . InlineShapes :
if inline_shape . Type == win32com . client . constants . wdInlineShapeChart : # 检查是否为内嵌图表
shape = doc . InlineShapes ( idx )
# 获取图表的标题
# print(shape.Chart.ChartTitle.Text)
sheet = shape . Chart . ChartData . Workbook . Worksheets ( " Sheet1 " )
# 行数
row_size = sheet . UsedRange . rows . Count
# 列数
col_size = sheet . UsedRange . columns . Count
# 遍历获取表格中的数据
for i in range ( 1 , row_size + 1 ) :
for j in range ( 1 , col_size + 1 ) :
print ( sheet . Cells ( i , j ) . Value , end = " " )
print ( " " )
print ( " " )
idx = idx + 1
# 这里可以对图表进行操作,比如获取图表的类型、数据等
doc . Close ( )
docApp . Quit ( )