I believe that neither the openpyxl
, nor any other XLSX file creation library (or ODS, which are also spreadsheets) provides a level of control over image positioning and resizing and style-they all focus more on the contents of cells.
You can try some experiments with the (style) style of the cell where you put the image - but the Openpyxl documentation doesn’t help much - https://openpyxl.readthedocs.io/en/stable/styles.html
You have two other approaches to try to evolve your problem
1 - Fix worksheet XML "manually"
Both "xlsx" and "ods" files are composed of a "zip" of XML files, where the content really is. These XML will have several specific attributes and tags where are stored the parameters of positioning and formatting of items, which are usually edited manually in the worksheet program (in this case, Excel).
Then the suggestion would be, after creating a spreadsheet as you do above, open it in excel, position the image as you want, and save as another spreadsheet -
decompress the two worksheets, side by side, (in windows you can decompress with the same program that treats zip files - be it Winrar, Winzip, etc..., you may need to rename the worksheet to change the extension), and, with the worksheets uncompressed, try to locate the difference in XML for formatting your image - may be one or more tags, and several attributes -
at that point, if you can identify the attributes that excel uses to position and give the image size it may be possible to use the features of style from Openpyxl to format the image (as it is above). This certametne would be the most viable path of all!
Otherwise, check if manipulating xml through Python can recreate and change the tags in XML as it is generated by Openpyxl to arrive at the same values as Excel (use the library zipfile
to "open" the xlsx and xml.etree
to read and change the xml, and then zipfile
to recreate the xlsx.
It won’t be an easy task!
2 - Programmatically interact with Excel
Generate the spreadsheet with Openpyxl normally, and then use direct interaction of Python with Excel opened through win32con - which allows you to call "functions" of the Excel interface from Python, to have excel open your spreadsheet, position the image as needed, and save her back.
It might be less work - but it’s gonna be pretty boring too.
Here is a list of resources and libraries to work with integrating Python and Excel - some of them may even allow image placement more directly:
https://www.pyxll.com/blog/tools-for-working-with-excel-and-python/
I found how to do it, even much easier and practical than I thought: Ws.PageSetup.Centerheaderpicture.Filename = (r'C: Users daymon.rebac Routines IMAGES Excel Horizontal header.png') Ws.PageSetup.Centerheader = "&G" Ws.PageSetup.Centerfooterpicture.Filename = (r'C: Users daymon.rebac Routines IMAGES Horizontal Excel Footer.png') Ws.PageSetup.Centerer = "&G" And then just adjust the margins of the page: Ws.PageSetup.Margin = 50.0&#leftXa; Ws.PageSetup.Topmargin = 70.0 Ws.PageSetup.Rightmargin = 50.0 Ws.PageSetup.Zoom = False
– Daymon Rebac
you can post this comment as a reply and accept it - helps anyone who comes after (and you still earn points). Browsing the openpyxl documentation, I haven’t seen these methods, so if together you put direct links to these parts of the documentation, it helps even more.
– jsbueno