我需要在Word中插入SVG文件。因为,我们不能直接这样做,所以我计划将SVG转换为EMF并插入它。使用inkscape可以很好地将SVG转换为EMF。然而,我无法找到将其插入Word的正确代码。 我按照Alvaro在this post中解释的步骤进行操作。已在所附文件中显示了遵循的步骤-
这是我的密码-
但是,当我运行附件中显示的代码时,它仍然抛出docx.image.exceptions.UnrecognizedImageError。github上的库的贡献者声称该库解决了这个问题。如果是这样的话,请让我知道,如果我错过了什么
我能够手动成功插入EMF文件。通过插入EMF来附加文档。此EMF从the internet下载进行测试
SVG可以直接添加到Word中-只需在Word(2016)中手动试用即可。 我已经为您的用例创建了一个example Java project作为POC。 无需调用inkscape,因为回退PNG是通过Batik动态创建的
当然,OP要求提供Python解决方案——但是如果Python openxml缺少一些功能,那么可能需要付出更多的努力,通过Python来运行它,而不是调用java运行时
关于通过EMF的变通解决方案,请注意,在EMF渲染器(我在POI中实现)中,有多种确定边界的方法,默认情况下,我扫描窗口和视口记录,只有在找不到任何其他内容或通过配置选项忽略扫描时,才使用EMF头边界。这通常会给我更好的结果
示例项目的相关代码snipplet如下所示:
模块
docx
似乎无法处理EMF
文件我的意思是,围绕这一点开展的工作如下:
docx文件的典型结构:
它将文档文件
doc.docx
的内容解压缩到临时文件夹_temp
,然后用当前目录中的另一个文件new_image.emf
替换临时目录中的文件image1.emf
。然后它将临时文件夹的内容打包回doc_new.docx
文件并删除临时目录注意:新图像在
new_doc.docx
中的大小与旧图像相同因此,工作流程可以是这样的:创建模板docx文件,手动将模板emf图片放在那里,然后保存docx文件。然后获取新的emf图像,将该图像放在docx文件旁边并运行脚本。通过这种方式,您可以获得一个带有新emf映像的新docx文件
我想您有很多emf图像,所以在这个脚本中添加几行代码是有意义的,这样它就可以拍摄多个图像并生成多个docx文件
如果所有emf图像的大小都相同,那么它就可以正常工作。如果它们的大小不同,则需要更多的编码来处理xml数据
更新
我已经知道了如何获得emf图像的大小。下面是完整的解决方案:
下面是另一个基于
win32com
模块和MS Word API的解决方案:将EMF图像放入子文件夹
pictures
并运行此脚本。之后,您将在当前文件夹中获得包含所有这些EMF图像的文件pictures.docx
相关问题 更多 >
编程相关推荐