Search code examples
pythonattributeerrorpython-docx

AttributeError: 'str' object has no attribute 'add_picture'


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


Solution

  • 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)