无法使用 Python 将 EMF 插入 Word [英] Unable to insert EMF into Word using Python

查看:59
本文介绍了无法使用 Python 将 EMF 插入 Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 SVG 文件插入 Word.由于我们无法直接执行此操作,因此我计划将 SVG 转换为 EMF 并插入它.使用inkscape 从SVG 到EMF 的转换工作正常.但是,我无法想出将其插入 Word 的正确代码.我按照在

I have a requirement for inserting SVG file into Word. Since, we cannot do this directly I am planning to convert SVG to EMF and insert it. Conversion from SVG to EMF works fine using the inkscape. However, I am unable to come up with right code for inserting it into Word. I followed the steps explained the the person Alvaro in

这是我的代码 -

但是,当我运行附件中显示的代码时 - 它仍然抛出 docx.image.exceptions.UnrecognizedImageError.github 上库的贡献者声称该库解决了这个问题.如果是这样,请让我知道我是否遗漏了什么.

However, when I run the code shown in the attachment - It still throws docx.image.exceptions.UnrecognizedImageError. The Contributor of the library on github claims that this library addresses this issue. If so then please let me know if I am missing anything.

我可以手动成功插入 EMF 文件.通过插入 EMF 附加文档.此 EMF 是从互联网下载用于测试.

I am able to insert the EMF file successfully manually. Attaching the doc by inserting the EMF. This EMF was downloaded from the internet for testing.

推荐答案

这里是另一个基于 win32com 模块和 MS Word API:

Here is another solution based on win32com module and MS Word API:

from pathlib import Path
import win32com.client

cur_dir  = Path.cwd()                                   # get current folder
pictures = list((cur_dir / "pictures").glob("*.emf"))   # get a list of pictures
word_app = win32com.client.Dispatch("Word.Application") # run Word
doc      = word_app.Documents.Add()                     # create a new docx file

for pict in pictures:                                   # insert all pictures
    doc.InlineShapes.AddPicture(pict)

doc.SaveAs(str(cur_dir / "pictures.docx"))              # save the docx file
doc.Close()                                             # close docx
word_app.Quit()                                         # close Word

将您的 EMF 图像放在子文件夹 pictures 中并运行此脚本.之后,您将在当前文件夹中获得包含所有这些 EMF 图像的文件 pictures.docx.

Put your EMF images in subfolder pictures and run this script. After that you get in current folder the file pictures.docx that contains all these EMF images inside.

这篇关于无法使用 Python 将 EMF 插入 Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆