image - 如何在 python-docx 中使用 add_picture() 在背景上插入图像(与文本重叠)?
问题描述
如何通过 python-docx 将图像插入到文本中的背景(如重叠)?
我知道图像可以放置在右侧或左侧的文本。
但我想要在任何文本上浮动图像之类的东西。像背景图像。
有可能吗?
谢谢。
解决方案
python-docx
v0.8.10尚不支持浮动图片。但是基于创建内嵌图片的实现,我探索了一种解决方法,并在我的项目中使用。
https://github.com/dothinking/pdf2docx/issues/54#issuecomment-715925252
探索浮动图像的步骤:
- 创建两个 docx 文件,一个插入内联图像,另一个插入浮动图像(在本例中为使用
behind text
模式) - 检查这两个文件之间源xml的差异
- 基于观察到的结构和
python-docx
内联图像源代码实现浮动图像
xml结构结果:
- 内联图像是
<wp:inline>
下的一个节点<w:drawing>
- 浮动图像是
<wp:anchor>
下一个节点<w:drawing>
- 除了内嵌图像的所有子节点,浮动图像还包含
<wp:positionH>
和<wp:positionV>
定义固定位置
所以,想法是创建<wp:anchor>
节点,然后附加子节点:不仅是所有具有内联图像的相同节点,而且还有额外的<wp:positionH>
和<wp:positionV>
。
# -*- coding: utf-8 -*-
'''
Implement floating image based on python-docx 0.8.10.
- Text wrapping style: BEHIND TEXT <wp:anchor behindDoc="1">
- Picture position: top-left corner of PAGE `<wp:positionH relativeFrom="page">`.
Create a docx sample (Layout | Positions | More Layout Options) and explore the
source xml (Open as a zip | word | document.xml) to implement other text wrapping
styles and position modes per `CT_Anchor._anchor_xml()`.
'''
from docx.oxml import parse_xml, register_element_cls
from docx.oxml.ns import nsdecls
from docx.oxml.shape import CT_Picture
from docx.oxml.xmlchemy import BaseOxmlElement, OneAndOnlyOne
# refer to docx.oxml.shape.CT_Inline
class CT_Anchor(BaseOxmlElement):
"""
``<w:anchor>`` element, container for a floating image.
"""
extent = OneAndOnlyOne('wp:extent')
docPr = OneAndOnlyOne('wp:docPr')
graphic = OneAndOnlyOne('a:graphic')
@classmethod
def new(cls, cx, cy, shape_id, pic, pos_x, pos_y):
"""
Return a new ``<wp:anchor>`` element populated with the values passed
as parameters.
"""
anchor = parse_xml(cls._anchor_xml(pos_x, pos_y))
anchor.extent.cx = cx
anchor.extent.cy = cy
anchor.docPr.id = shape_id
anchor.docPr.name = 'Picture %d' % shape_id
anchor.graphic.graphicData.uri = (
'http://schemas.openxmlformats.org/drawingml/2006/picture'
)
anchor.graphic.graphicData._insert_pic(pic)
return anchor
@classmethod
def new_pic_anchor(cls, shape_id, rId, filename, cx, cy, pos_x, pos_y):
"""
Return a new `wp:anchor` element containing the `pic:pic` element
specified by the argument values.
"""
pic_id = 0 # Word doesn't seem to use this, but does not omit it
pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
anchor = cls.new(cx, cy, shape_id, pic, pos_x, pos_y)
anchor.graphic.graphicData._insert_pic(pic)
return anchor
@classmethod
def _anchor_xml(cls, pos_x, pos_y):
return (
'<wp:anchor distT="0" distB="0" distL="0" distR="0" simplePos="0" relativeHeight="0" \n'
' behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" \n'
' %s>\n'
' <wp:simplePos x="0" y="0"/>\n'
' <wp:positionH relativeFrom="page">\n'
' <wp:posOffset>%d</wp:posOffset>\n'
' </wp:positionH>\n'
' <wp:positionV relativeFrom="page">\n'
' <wp:posOffset>%d</wp:posOffset>\n'
' </wp:positionV>\n'
' <wp:extent cx="914400" cy="914400"/>\n'
' <wp:wrapNone/>\n'
' <wp:docPr id="666" name="unnamed"/>\n'
' <wp:cNvGraphicFramePr>\n'
' <a:graphicFrameLocks noChangeAspect="1"/>\n'
' </wp:cNvGraphicFramePr>\n'
' <a:graphic>\n'
' <a:graphicData uri="URI not set"/>\n'
' </a:graphic>\n'
'</wp:anchor>' % ( nsdecls('wp', 'a', 'pic', 'r'), int(pos_x), int(pos_y) )
)
# refer to docx.parts.story.BaseStoryPart.new_pic_inline
def new_pic_anchor(part, image_descriptor, width, height, pos_x, pos_y):
"""Return a newly-created `w:anchor` element.
The element contains the image specified by *image_descriptor* and is scaled
based on the values of *width* and *height*.
"""
rId, image = part.get_or_add_image(image_descriptor)
cx, cy = image.scaled_dimensions(width, height)
shape_id, filename = part.next_id, image.filename
return CT_Anchor.new_pic_anchor(shape_id, rId, filename, cx, cy, pos_x, pos_y)
# refer to docx.text.run.add_picture
def add_float_picture(p, image_path_or_stream, width=None, height=None, pos_x=0, pos_y=0):
"""Add float picture at fixed position `pos_x` and `pos_y` to the top-left point of page.
"""
run = p.add_run()
anchor = new_pic_anchor(run.part, image_path_or_stream, width, height, pos_x, pos_y)
run._r.add_drawing(anchor)
# refer to docx.oxml.shape.__init__.py
register_element_cls('wp:anchor', CT_Anchor)
if __name__ == '__main__':
from docx import Document
from docx.shared import Inches, Pt
document = Document()
# add a floating image
p = document.add_paragraph()
add_float_picture(p, 'test.png', width=Inches(5.0), pos_x=Pt(20), pos_y=Pt(30))
# add text
p.add_run('Hello World'*50)
document.save('output.docx')
推荐阅读
- html - 如何从我的网页中删除 Bootstrap 缩略图
- python - 将numpy中的单列JSON拆分为多列数组
- tensorflow - ValueError:形状必须为 2 级,但对于输入形状为 [6]、[6] 的“MatMul”(操作:“MatMul”)为 1 级
- apache-kafka - 如何从 Spark 结构化流中的特定 Kafka 分区中读取数据
- sql - 查询以获取 2 个表之间的匹配记录并且不匹配来自第二个表 2 的 Null 记录
- react-native - 什么是差异状态和构造函数?
- python - 多线程以在 Python 中更快地下载图像
- php - Codeigniter 电子邮件在服务器 fsockopen() 中不起作用:无法连接到 ssl://smtp.googlemail.com:465(连接被拒绝)
- java - ServletRequestListner 是否适用于邮递员的 REST api 调用?
- javascript - 我创建了一个 aspx 页面,我想在其中检查数据是否存在于 gridview 中或不使用 javascript,但该函数没有被执行