pywin32库 -- 读取word文档中的图形

文章目录

前置操作

基于pywin32打开、关闭word应用程序;

python 复制代码
import pythoncom
from win32com.client import Dispatch, GetActiveObject


def get_word_instance():
    """ 获取word进程 实例"""
    pythoncom.CoInitialize()
    try:
        # 获取运行的Word实例
        word_app = GetActiveObject("Word.Application")
    except pythoncom.com_error:
        # 打开word程序
        word_app = Dispatch("Word.Application")
        word_app.Visible = False  # 不显示 Word 界面
        word_app.DisplayAlerts = False
    finally:
        return word_app


def close_word_instance():
    try:
        word = GetActiveObject("Word.Application")
        if word:
            word.Quit()
    except pythoncom.com_error:
        # 杀掉word进程
        pass

解析body中的图形

python 复制代码
graphics = {}
import os
from PIL import ImageGrab, Image


def get_graphic_with_pywin32(doc_path):
    """ 基于pywin32 解析文档主体中的图形 """
    global graphics
    word = get_word_instance()
    doc = word.Documents.Open(doc_path)
    for shape in doc.Shapes:  # 文档主体中的图形
        print("shape:", shape.Name, shape.Type)  # Type为1是图形,Name唯一
		page_id = shape.Anchor.Information(1)
        # shape.Anchor.CopyAsPicture()   个人版不支持
        # image = ImageGrab.grabclipboard()

        # 方案1  图形转图片
        inline_shape = shape.ConvertToInlineShape()
        bdata = inline_shape.Range.EnhMetaFileBits.tobytes()
        from io import BytesIO
        img = Image.open(BytesIO(bdata))
        img.save(shape.Name + ".png")

        # 方案2,若以上方案获取的图片 纵横比失真,则采用该方案
        # inline_shape = shape.ConvertToInlineShape()
        # inline_shape.Range.CopyAsPicture()
        # image = ImageGrab.grabclipboard()

        # 方案3, 图形直接保存   (个人版 报错AttributeError: <unknown>.SaveAsPicture)
        # pic_path = os.path.abspath("./{}_3.png".format(shape.Name))
        # shape.SaveAsPicture(pic_path)  # 绝对路径

解析页眉中的图形

python 复制代码
def get_graphic_with_pywin32(doc_path):
    """ 基于pywin32 解析文档主体中的图形 """
    global graphics
    word = get_word_instance()
    doc = word.Documents.Open(doc_path)
    for section in doc.Sections:
        for header in section.Footers:
            for shape in header.Shapes:
                inline_shape = shape.ConvertToInlineShape()
                bdata = inline_shape.Range.EnhMetaFileBits.tobytes()  # 直接保存无法查看
                img = PillowImage.open(BytesIO(bdata))
                img.save("./{}.png".format(shape.Name))
                with open("./{}.png".format(shape.Name), "rb") as f:
                    bdata = f.read()  # 读取的字节 与 image.tobytes() 不一样
                graphics[shape.Name] = bdata  # Name唯一
相关推荐
缺点内向1 天前
告别“复制粘贴”:用C#和模板高效生成Word文档
开发语言·c#·word
初九之潜龙勿用1 天前
C#实现导出Word图表通用方法之散点图
开发语言·c#·word·.net·office·图表
私人珍藏库1 天前
[吾爱大神原创工具] Word图片批量导出并插入Excel对应单元格
word·excel
a程序小傲1 天前
SpringBoot 秒实现在线 Word 编辑、协同、转化等功能
java·开发语言·spring boot·后端·spring·word·深度优先
a程序小傲1 天前
国家电网Java面试被问:API网关的JWT令牌验证和OAuth2.0授权码流程
java·开发语言·spring boot·后端·面试·职场和发展·word
gc_22992 天前
学习C#调用OpenXml操作word文档的基本用法(17:学习文档图片类)
c#·word·图片·openxml
2501_930707782 天前
使用C#代码在 Word 文档页面中添加装订线
开发语言·c#·word
miaobinfei2 天前
pdf转word,图片文字转word(使用OCR工具)
pdf·ocr·word
513495923 天前
在Vue.js项目中使用docx和file-saver实现Word文档导出
前端·vue.js·word
缺点内向3 天前
C# 高效统计 Word 文档字数:告别手动,拥抱自动化
c#·自动化·word