79536522

Date: 2025-03-26 13:17:23
Score: 0.5
Natty:
Report link

Yes, you could export data to word doc. Here is the code:

import statsmodels.api as sm
import numpy as np
import pandas as pd
from docx import Document

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

summary = model.summary2().tables[1]

doc = Document()
doc.add_heading('Regression Results', level=1)

table = doc.add_table(rows=summary.shape[0] + 1, cols=summary.shape[1])
table.style = 'Table Grid'

for j, col_name in enumerate(summary.columns):
    table.cell(0, j).text = col_name

for i in range(summary.shape[0]):
    for j in range(summary.shape[1]):
        table.cell(i + 1, j).text = str(round(summary.iloc[i, j], 4))

doc.save("Regression_Results.docx")
print("Regression table successfully exported to 'Regression_Results.docx'")

Output:

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Subir Chowdhury