Skip to content

Latest commit

 

History

History
317 lines (236 loc) · 8.31 KB

483-2576045-插入图片_浮于文字上方_xml节点.sy.md

File metadata and controls

317 lines (236 loc) · 8.31 KB
show version enable_checker
step
1.0
true

设置图片 版式

回忆

  • 上次综合运用python
    • 生成了一篇文档
    • 你可以生成一篇 属于自己的文档吗?
  • 如果想要设置插入图片的版式怎么办呢?

插入图片

from docx import Document
from docx.shared import Pt

document = Document()
p = document.add_paragraph()
r = p.add_run("before")
r.add_picture('m.png')
p.add_run("after")
document.save("o.docx")
  • 图片模式为行内模式

图片描述

  • 观察document.xml

观察

unzip -d oeasy oeasy.docx
cd oeasy/word
firefox document.xml
  • 文档分成三段
  • 图片在于中心

图片描述

  • 如果想要将图片做成其他版式
  • 应该如何呢?

切换版式

  • 在libreoffice中
    • 重新插入图片
    • 并设置版式

图片描述

  • 然后设置位置

图片描述

  • 再次观察

浮于文字下方

unzip -d oeasy o.docx
cd oeasy/word
firefox document.xml
  • 新的drawing 自起一run

图片描述

  • wp:anchor节点的behindDoc属性表明
    • 图片版式为衬于文本下方
  • wp:positionH和wp:positionV节点
    • 表明水平和竖直绝对定位方式
    • relativeFrom属性指定用于定位的参考对象
    • 子节点wp:posOffset指定具体坐标值

查询位置

pip3 show python-docx
  • 查看库

图片描述

进入目录

cd /home/shiyanlou/.local/lib/python3.8/site-packages
grep add_picture *.py
  • 查询add_picture的定义

图片描述

  • 在document.py中

观察函数定义

vi document.py
  • 查找函数定义
    • /def add_picture

图片描述

  • document.add_picture
    • 调用了run.add_picture

深入

  • 找到text/run.py

图片描述

  • 继续深入

查找

grep -r "def new_pic_inline" --include="*.py"
  • 找到位置

图片描述

编辑

vi parts/story.py
  • 找到函数
    • /new_pic_inline

图片描述

  • 需要找到CT_Inline

深入查找

grep -r "class CT_Inline" --include="*.py"

图片描述

vi oxml/shape.py
  • 这个应该是操作xml节点的具体执行函数了

具体函数

图片描述

  • 100行以上
    • 都是对xml的直接设置

开始设置

cd ~
vi add_float_picture.py
  • 编辑模块
# -*- 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 | 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.__init__.py
register_element_cls('wp:anchor', CT_Anchor)

编辑主程序

from docx import Document
from docx.shared import Inches, Pt
from add_float_picture import add_float_picture

if __name__ == '__main__':

    document = Document()

    # add a floating picture
    p = document.add_paragraph()
    add_float_picture(p, 'm.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')
  • 最终效果

图片描述

总结🤔