How to vertically align picture in line using python-docx -
i adding picture (some latex converted png using matplotlib) text using following code:
par = doc.add_paragraph() par.add_run().text = 'foo bar baz' par.add_run().add_picture('pic.png') par.add_run().text = 'blah blah blah'
this works ok, except picture pic.png
not vertically aligned in rest of text in document:
i can alignment manually in ms word adding character style advanced vertical alignment property set "lowered 10pt":
the problem have no idea how programatically using python-docx. conceptually steps compute size of image, create character style lowered half size minus half size of font , apply style run containing picture. how create raised or lowered font style in python-docx?
your image has large (transparent) border around it. added single pixel border inside extents here make visible:
i expect word aligning bottom of image baseline (as expected). 1 approach see if there way specify 0 bottom border.
you try subscript on image run. i'm not sure it's worth try. this:
run = par.add_run() run.add_picture('x.png') run.font.subscript = true
if find run manually set "lowered 10pt", can view xml (aircode):
run = vertically_adjusted_run() # ahold of print(run._element.xml)
i expect you'll see this:
<w:r> <w:rpr> <w:position w:val="20"/> ...
... w:position element sets adjustment baseline. value specified in half-points.
anyway, neither adjustment nor low-level element supported python-docx yet, you'd need in there lxml calls needful if wanted badly enough.
Comments
Post a Comment