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.
39 lines
1.3 KiB
39 lines
1.3 KiB
9 months ago
|
# 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:/昭通市.docx")
|
||
|
|
||
|
index_tubiao = 2
|
||
|
|
||
|
# 遍历文档中的所有内嵌形状
|
||
|
idx = 1
|
||
|
for inline_shape in doc.InlineShapes:
|
||
|
if inline_shape.Type == win32com.client.constants.wdInlineShapeChart: # 检查是否为内嵌图表
|
||
|
if idx == index_tubiao:
|
||
|
shape = doc.InlineShapes(idx)
|
||
|
# 获取图表的标题,此项目中图表没有标题
|
||
|
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
|
||
|
|
||
|
# 关闭文档和Word应用
|
||
|
doc.Close()
|
||
|
docApp.Quit()
|