Problem insert_image : scale image and cell format
See original GitHub issueHi,
I am using XlsxWriter to do set image with my size (with scale parameters) on excel but when the cells format on row context have a cell format (wrap text or police), the scaling change at opening file.
I am using Python version 3.7 and Excel version 19.09 via 365.
Here is some code that demonstrates the problem:
import xlsxwriter
import base64
import io
def main():
_PICTURE_BASE64 = "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCACbALQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD8X6KKK/ynP+/gKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/2Q=="
bytes_data = base64.b64decode(_PICTURE_BASE64)
data_img = io.BytesIO(bytes_data)
original_height = 5.47
original_width = 6.35
height = 4.48
width = 5.14
workbook = xlsxwriter.Workbook("output_img.xlsx")
worksheet = workbook.add_worksheet()
x_scale = width / original_width
y_scale = height / original_height
height_manualy = 126
worksheet.set_row(0, height_manualy)
worksheet.insert_image(0, 1, 'Image 1',
{'image_data': data_img, 'y_scale': y_scale, 'x_scale': x_scale, 'object_position': 2})
cell_format = workbook.add_format({'size': 50})
worksheet.write(2, 0, "Text\nText2", cell_format)
worksheet.insert_image(2, 1, 'Image 2',
{'image_data': data_img, 'y_scale': y_scale, 'x_scale': x_scale, 'object_position': 2})
workbook.close()
if __name__ == '__main__': main()
I read the source code, and i understand, is the a problem like autofit engineering, xlsxwriter use row/col sizes informations to know size and calculate width and height img.
I can to get around with set_row height (like from example Image 1) but, i need to know with the size img to convert mm/pixels to unit of excel to do like Image 1 on my example)
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
python - Set width and height of an image when inserting via ...
You can scale the image externally or within Excel using XlsxWriter and the x_scale and y_scale based on the the heights and widths...
Read more >How to insert picture in Excel cell, comment, header and footer
On the Format Picture pane, switch to the Size & Properties tab, and select the Move and size with cells option. Lock the...
Read more >How to Insert Picture & Auto Resize with Excel Cell - YouTube
Insert image and auto resize when you resize excel cellsClick here for more ...
Read more >How to Insert Image in Excel Cell (Correctly) - YouTube
In this video tutorial, you'll learn how to insert an image in an Excel cell, in the right way. Each image inserted in...
Read more >Microsoft Excel: How to insert an image into a cell
Next, right-click the image and select Format Picture from the pop-up menu and, in the resulting dialog box, select the Size & Properties...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry for not getting back to you on this.
If you have a working solution that is good.
If I was doing this I would work in pixels and make sure you take the DPI scaling into account (which I think you do).
At some stage I plan to make the XlsxWriter image metric routines public to make it easier to deal with these types of cases.
Hi, My solution (with height_final_cm and width_final_cm variable):