stargazer icon indicating copy to clipboard operation
stargazer copied to clipboard

Option to render as an excel file/pandas table

Open NicolasWoloszko opened this issue 3 years ago • 8 comments

Excel file remains the easier format for a table to be inserted in a word document... Or a pandas table ?

NicolasWoloszko avatar Sep 20 '21 13:09 NicolasWoloszko

I suspect preparing all numerical values (coefficients, stats...) in a Pandas table, and possibly saving it to Excel, would not be difficult (although it probably requires a different code flow compared to the currently supported formats).

What seems to me significantly more difficult is formatting the table so that it actually looks nice inside a document, including the borders, but also the alignment, numbers formatting (e.g. number of digits)... this can't be done through Pandas, requires good (better than mine, at least) knowledge of xlwt or similar, and presumably needs a rewrite for each format (xls vs. xlsx vs. ods).

So all this said, even if I found the time to implement the first part, I think the best approach to including a table in a Word document would still be either to copy from a browser (e.g. jupyter notebook) the html generated by stargazer, or to directly include a pdf generated by stargazer.

In any case, it might be that I'm too pessimistic and I'm ready to accept contributions.

toobaz avatar Sep 20 '21 14:09 toobaz

@NicolasWoloszko and @toobaz, I took the liberty to start working on an excel renderer. Let me know if something like the below is what you are looking for!

Currently I am working with the example data from the test file:

import pandas as pd
import statsmodels.formula.api as smf

from stargazer.stargazer import Stargazer

df = pd.DataFrame(list(zip(range(9), range(0, 18, 2))),  columns =['a', 'b'])
est1 = smf.ols('a ~ 0 + b', df).fit()
est2 = smf.ols('a ~ 1 + b', df).fit()
stargazer = Stargazer([est1, est2])

Calling the excel renderer works similar to the html and LaTeX renderers:

stargazer.render_excel(filename='test.xlsx')

Which returns the following table in the test.xlsx file:

stargazer_excel_output_example

robbertjan94 avatar Feb 01 '22 08:02 robbertjan94

Cool! Is it based on openpyxl?

I can take a look at the code when you want, but consider splitting the code to a separate file if you like - we can even move the html and LaTeX renderers to separate files for consistency.

toobaz avatar Feb 03 '22 22:02 toobaz

Is render_excel already merged with the main? The code does not seem to be working for me.

achinmay17 avatar Feb 04 '22 05:02 achinmay17

Cool! Is it based on openpyxl?

Thanks. No, I actually use the xlsxwriter package. Do you have a preference for any specific package or is this one fine too?

I can take a look at the code when you want, but consider splitting the code to a separate file if you like

The example I showed you is generated by a prototype. I will work on it a bit more and share the code here for feedback.

we can even move the html and LaTeX renderers to separate files for consistency.

I'll take a look at the structure of the package and whether branching out to separate files would work.

robbertjan94 avatar Feb 04 '22 07:02 robbertjan94

Is render_excel already merged with the main? The code does not seem to be working for me.

Hi @achinmay17, thanks for your interest in the excel renderer. Currently it is still a work in progress and therefore not merged with the main. But given the positive feedback I will put some extra effort in to get it finished!

robbertjan94 avatar Feb 04 '22 07:02 robbertjan94

Thanks. No, I actually use the xlsxwriter package. Do you have a preference for any specific package or is this one fine too?

I have no strong preference. openpyxl looks to me slightly more established, but if you found yourself at ease with xlsxwriter please go on. Both are supported by pandas, by the way, which is a vague indication that both should do their job well.

toobaz avatar Feb 04 '22 09:02 toobaz

... I can take a look at the code when you want, but consider splitting the code to a separate file if you like ...

@toobaz I have created a PR for you to check. For now I have kept all renderers in the same file but we can definitely split them into separate files if you'd like to so.

robbertjan94 avatar Feb 09 '22 15:02 robbertjan94