python-docx插⼊图⽚调整为封⾯图
因需要添加word封⾯图,并未到相关资料直接添加封⾯图,操作word插⼊封⾯图后想到⼀个解决办法就是插⼊图⽚修改⼤⼩使其成为封⾯图。
查资料后遇到⼀个问题就是如何修改图⽚环绕⽅式,经多⽅查后到⼀篇⽂章,简单的思路就是添加xml
docx背后的xml
我们还知道,docx⽂档的背后是xml格式的数据,python-docx正是通过处理xml的⽅式来读写word⽂档。所以,接下来先⼿⼯创建word ⽂档,然后查看图⽚部分的xml内容。
作为对⽐,⾸先分别创建⼀个普通嵌⼊型图⽚⽂件和⼀个衬于⽂本下⽅的浮动型图⽚⽂件。然后执⾏查看步骤:右键docx⽂件 | 7-zip打开压缩包 | word | l,复制⽂件内容并格式化xml,得到如下的关于图⽚部分的⽚段。为了便于对⽐分析,删除了⼀些节点属性。(具体内容⽂章底部会有源⽂链接)
pip install python-docx
# -*- coding: utf-8 -*-
# filename: add_float_picture.py
'''
Implement floating image based on python-docx.
- 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 | l) to implement other text wrapping
styles and position modes per `CT_Anchor._anchor_xml()`.
'''
l import parse_xml, register_element_cls
l.ns import nsdecls
l.shape import CT_Picture
l.xmlchemy import BaseOxmlElement, OneAndOnlyOne
# refer l.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 passedgetsavefilename
as parameters.
"""
anchor = parse_xml(cls._anchor_xml(pos_x, pos_y))
< = cx
< = cy
anchor.docPr.id= shape_id
anchor.docPr.name ='Picture %d'% shape_id
'/drawingml/2006/picture'
)
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
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 = w(pic_id, filename, rId, cx, cy)
anchor = w(cx, cy, shape_id, pic, pos_x, pos_y)
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.w_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 = _or_add_image(image_descriptor)
cx, cy = image.scaled_dimensions(width, height)
shape_id, filename = _id, image.filename
return w_pic_anchor(shape_id, rId, filename, cx, cy, pos_x, pos_y)
# refer un.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 l.__init__.py
register_element_cls('wp:anchor', CT_Anchor)
document = Document(path)
pic = document.paragraphs[0]#其实位置添加
add_float_picture(pic, img_path, width=Cm(21.29), height=Cm(30.18), pos_x=Cm(0.11), pos_y=Cm(-0.2)) # 判断⽂件是否存在,不存在则创建
save_file_path = settings.MEDIA_ROOT +f'/userfile/{userID}/report_ini/'
if not ists(save_file_path):
os.makedirs(save_file_path)
save_file_name = get_save_filename()
document.save(save_file_path + save_file_name)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论