[Python] Exporting regression tables from statsmodels into Word

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am making a lot of regression models in Python. I want to report the results in tables in a word-file. Is there any way I can export the results into nicely formatted tables in word, e.g., into a .docx-file?

    stargazer offers great oppurtunities for customizing the tables which I need. It can export into LateX or html - but this is not what I need.

    The best solution I can come up with now is to export into html using, then copy-paste into word, but that still requires doing some final modifcations to the tables when they are pasted into Word. And I want to automate as much of the process as possible, since I have MANY tables.

    Can I create the tables in a Microsoft Word Document using only code?

    import statsmodels.api as sm
    import numpy as np
    import pandas as pd

    np.random.seed(123)
    data = pd.DataFrame({
    'X1': np.random.randn(100),
    'X2': np.random.randn(100),
    'Y': np.random.randint(0, 2, 100)
    })

    model = sm.Logit(data['Y'], sm.add_constant(data[['X1', 'X2']])).fit()

    Continue reading...

Compartilhe esta Página