-2

I need to insert a picture(png format and named as {number2}.png) in the word(named as {number2}.docx), but it doesn't work and shows this error, someone can help me?enter image description here

Danny
  • 3
  • 2
  • Welcome to SO. doc2 is a simple string. There is no way to add a picture to it. I suppose you should open the file, understand its format, before trying adding something to it. – Malo Mar 21 '21 at 11:21

1 Answers1

0

On your line:

doc2 = f'{number2}.docx'

you are assigning the value '{number2}.docx' to doc2. '{number2}.docx' is a string - for example if number2 = "hello" then doc2 = "hello.docx".

I'm not sure about the add_picture part, but I'm assuming you may want something like:

doc2 = open(f'{number2}.docx')

or

with open(f'{number2}.docx'):

    # code

I am not sure if this works, or if you are using another module for the adding picture? (I don't have enough reputation to comment)

  • I have multiple word documents and pictures to operate it. Like inserting 1.jpg to 1.docx, and 2.jpg to 2.docx So, I want to code it to repeat this operation. In this way, the picture name will change according to the name of the document. – Danny Mar 21 '21 at 11:39
  • 1
    I got it after change the codding of open docx part! thx – Danny Mar 21 '21 at 12:08