[Python] Add plots directly to a document using python package docx

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am plotting some data and I want to automatically generate a report. I can save the plot and then add it to my document. However, I prefer to do it directly, without saving step. Going through the python-docx documentation I doubt it is doable by this package. Is there another way?

    My code looks like this right now

    from docx import Document
    from docx.shared import Inches
    import matplotlib.pyplot as plt
    import numpy as np

    t = np.arange(0.0, 2.0, 0.01)
    s = 1 + np.sin(2*np.pi*t)
    plt.plot(t, s)
    plt.savefig('test.png')

    document = Document()
    document.add_heading('Report',0)
    document.add_picture('test.png', width=Inches(1.25))

    document.save('report.docx')

    Continue reading...

Compartilhe esta Página