要保留Word文档中的SVG原样,可以使用以下代码示例:
from docx import Document
# 打开Word文档
doc = Document('path_to_your_word_document.docx')
# 遍历文档中的所有图像
for shape in doc.inline_shapes:
if shape.has_chart:
# 如果图像是图表,则跳过
continue
if shape.has_picture:
# 如果图像是图片,则跳过
continue
if shape.has_text_frame:
# 如果图像是文本框,则跳过
continue
# 获取图像的原始SVG数据
svg_data = shape._inline.graphic.graphicData.pic.graphic.graphicData.uri
# 将SVG数据保存为文件
with open('path_to_save_svg.svg', 'w') as f:
f.write(svg_data)
# 关闭Word文档
doc.close()
请将path_to_your_word_document.docx
替换为你要处理的Word文档的路径,将path_to_save_svg.svg
替换为你要保存SVG的路径。
这段代码将遍历Word文档中的所有图像,并将其中的SVG数据保存为文件。需要注意的是,这段代码只能处理内嵌的图像,而无法处理链接到外部文件的图像。