python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.63k stars 1.13k forks source link

Insert picture issue python-docx #1354

Open maxcavalli opened 8 months ago

maxcavalli commented 8 months ago

HI there I am running a python scripts that creates multiple docs using data from a spreadsheet and a word template. As part of the code I am determining based on the file name the organisation the docs needs to be generated and I am adding the logo in the header of the the word template...

    # Add a logo to the header of the document
    section = doc.sections[0]
    header = section.header
    p = header.paragraphs[0]
    run = p.add_run()
    logo_path = base_dir / f"{organization_name}_logo.jpg"
    run.add_picture(str(logo_path), width=Inches(1.25))

The documents are created fine. When I open the files I get this error:

"Word found unreadable content in "xxx.docx". Do you want to recover the contents of this document? If you trust the source of this document, click Yes. The logo is exactly where it should be and the appropriate one has been selected. I don't know how to address the error message. As I will need to convert these docs into PDFs the error will create issues. I am looking for help.

d3an commented 1 week ago

@maxcavalli Did you find a solution?

maxcavalli commented 1 week ago

@maxcavalli Did you find a solution?

Yes I have. thanks

KenWuqianghao commented 1 day ago

@maxcavalli Did you find a solution?

Yes I have. thanks

Can you please outline your solution here? I'm seeing the same issue.