Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to render as an excel file/pandas table #77

Open
NicolasWoloszko opened this issue Sep 20, 2021 · 8 comments · May be fixed by #83
Open

Option to render as an excel file/pandas table #77

NicolasWoloszko opened this issue Sep 20, 2021 · 8 comments · May be fixed by #83
Labels
enhancement New feature or request

Comments

@NicolasWoloszko
Copy link

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

@toobaz toobaz added the enhancement New feature or request label Sep 20, 2021
@toobaz
Copy link
Collaborator

toobaz commented Sep 20, 2021

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.

@robbertjan94
Copy link

robbertjan94 commented Feb 1, 2022

@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

@toobaz
Copy link
Collaborator

toobaz commented Feb 3, 2022

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.

@achinmay17
Copy link

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

@robbertjan94
Copy link

robbertjan94 commented Feb 4, 2022

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
Copy link

robbertjan94 commented Feb 4, 2022

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!

@toobaz
Copy link
Collaborator

toobaz commented Feb 4, 2022

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.

@robbertjan94 robbertjan94 linked a pull request Feb 5, 2022 that will close this issue
@robbertjan94
Copy link

robbertjan94 commented Feb 9, 2022

... 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants