There's more...

The size of the image can be changed, but as we saw before, the proportion of the image needs to be calculated if that changes. The resizing may end up not being perfect if done by approximation, as in Step 5 from the How to do it… section.

Notice that the image does not have a perfect ratio of 10:14. It should instead be 10:13.33. For an image, that may be good enough, but for data that is more sensitive to proportion changes, such as a chart, it may require extra care.

To obtain the proper relation, divide the height by the width and then scale properly:

>>> image = document.add_picture('photo-dublin-a1.jpg')
>>> image.height / image.width
0.75
>>> RELATION = image.height / image.width
>>> image.width = Cm(12)
>>> image.height = Cm(12 * RELATION)

If you need to transform the values to a particular size, you can use the cm, inches, mm, or pt attributes:

>>> image.width.cm
12.0
>>> image.width.mm
120.0
>>> image.width.inches
4.724409448818897
>>> image.width.pt
340.15748031496065

The whole python-docx documentation is available here: https://python-docx.readthedocs.io/en/latest/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.206.171