Pictures not attached in file
Question: I am using python win32 to drive excel 2010. I want to insert pictures into
certain cells

    
    
    import win32com.client as win32
    from win32com.client import constants as constants
    
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    excel.Visible = True
    
    wb = excel.Workbooks.Add()
    ws = wb.Worksheets('Sheet1')
    ws.Name = 'PicDemo' ## rename worksheet
    
    ## select the active sheet
    excel.Sheets('PicDemo').Select()
    ws = excel.ActiveSheet
    ws.Cells(9,9).Select()
    
    ## insert a png pic into this cell
    ws.Pictures().Insert(pic file path)
    

I can see the pictures inserted on my PC, but when I send the excel to my
friend, they couldn't see the picture but red cross with 'invalid link'.

I search the web and a lot of people complained about this glitch in excel
2010. I just want the pics saved absolutely without any dependent linking.

Is there a python win32 workaround?


Answer: Microsoft changed the behavior in Excel 2010 and 2013. Try this:

    
    
    xlApp = win32.Dispatch(r'Excel.Application')
    xlBook = xlApp.Workbooks.Open(filename)
    xlSheet = xlBook.Sheets(sheet_name)
    rng = xlSheet.Range("B2:F8")
    height = rng.Offset(rng.Rows.Count, 0).Top - rng.Top
    width = rng.Offset(0, rng.Columns.Count).Left - rng.Left
    xlSheet.Shapes.AddPicture(image_file_name, False, True, rng.Left, rng.Top, height, width)
    

[AddPicture](https://msdn.microsoft.com/en-us/library/office/ff198302.aspx)
has an option to save the image with the document.

Height and Width are required. In the code above I am using the Range to size
the image.

