1

I need to insert a picture into an excel cell comment box. Example of what I mean here.

Is there a way to do this with an existing python library?

I have looked at both openpyxl and xlsxwriter docs - it seems they just allow the creation of text comments.

Xlsx writer allows you to change the background color of a comment box, but it appears there is no solution for inserting a picture.

2 Answers 2

2

I wanted the same feature yet I couldn't find it in any python library.

I ended up using Excels "Macro" feature, you can find it under the "Developer" tab which you need to enable in File -> Options -> Customize Ribbon; it can be found on the right side amongst the Main Tabs.

After you select "Macro", give it a name and hit create it should create the sub for you where you can write this Visual Basic code:

Sub addPic()
'
' Macro3 Macro
'
    Dim pic_file As String
    Dim pic_resolution As Long
    Dim pict As Object
    
    If ActiveCell.Comment Is Nothing Then ActiveCell.AddComment
    pic_file = Application.GetOpenFilename("GIF (*.GIF), *.GIF", Title:="Select chord picture: ")
    Set pict = CreateObject("WIA.ImageFile")
    pict.loadfile pic_file
    pic_resolution = pict.VerticalResolution
    
  With ActiveCell.Comment.Shape
     .Fill.Visible = msoTrue
     .Fill.UserPicture (pic_file)
     .Height = pict.Height / pic_resolution * 96
     .Width = pict.Width / pic_resolution * 96
   End With
 
  Set pict = Nothing
   
  ActiveCell.Comment.Visible = False
End Sub

You can change the .GIF extension and resolution to whatever you need. Finally you should pick a keyboard shortcut which you can select under Macro->(Select your macro)->Options

Sign up to request clarification or add additional context in comments.

2 Comments

Great idea, but this does not answer the OP's question. How then to run this macro from Python's Excel APIs? So now we have to learn how to run a VBA code from Python - code injection sort of?
I think this can be considered as an answer. There is no way to do this over python. this saved me lots o time! Thanks to the A.
0

YOu can insert picture in spreadsheet using xlsxwriter. Check out this, it might help you to solve your issue.

2 Comments

Thanks but my issue is not just inserting a photo into a cell. I need to insert the picture into a comment box as shown in example. Any idea there?
Well, I dont think that there is provision to insert image in comment using xlsxwriter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.