question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem insert_image : scale image and cell format

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcnamaracommented, Nov 8, 2019

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.

0reactions
Clorelcommented, Nov 4, 2019

Hi, My solution (with height_final_cm and width_final_cm variable):

                # get_image_properties from inherit workbook class, with no setter on private object
                result = workbook.get_image_properties('None', value)

                width_px = result[1]
                height_px = result[2]
                dpi_w = result[4]
                dpi_h = result[5]

                width_in = width_px / dpi_w
                height_in = height_px / dpi_h

                conversion_in_to_cm = 2.54  # 25.4 to mm

                width_cm = width_in * conversion_in_to_cm
                height_cm = height_in * conversion_in_to_cm
                x_scale = width_final_cm / width_cm
                y_scale = height_final_cm / height_cm
                
                cell_column = int(command.attrib['idx_column_1']) - 1
                cell_row = int(command.attrib['idx_row_1']) - 1
                idx_worksheet = int(command.attrib['idx_sheet']) - 1

                # Define size image to stretch
                constante_points_by_cm = 28.35
                pixel_by_point_width = 0.75
                constante_points_width_by_cm = conversion_in_to_cm * constante_points_by_cm * pixel_by_point_width / 10
                height_pt = round(height_final_cm * constante_points_by_cm, 2)
                width_pt = round(width_final_cm * constante_points_width_by_cm, 2)

                # Set stretch
                worksheet.set_row(cell_row, height_pt)
                worksheet.set_column(cell_column, cell_column, width_pt)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found