Skip to content

ENH: Styler.to_pdf() and Styler.to_svg() #52455

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

Open
1 of 3 tasks
janosh opened this issue Apr 5, 2023 · 3 comments
Open
1 of 3 tasks

ENH: Styler.to_pdf() and Styler.to_svg() #52455

janosh opened this issue Apr 5, 2023 · 3 comments
Labels
Enhancement Needs Discussion Requires discussion from core team before further action Styler conditional formatting using DataFrame.style

Comments

@janosh
Copy link
Contributor

janosh commented Apr 5, 2023

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Styler already supports exporting to HTML. Wouldn't be a long shot from there to support exporting as SVG too which is accepted by many more programs like graphics editors and presentation software like PowerPoint and Keynote. The same is true for PDF. You can't drag and drop HTML into Keynote but you can PDFs.

Feature Description

styler.to_pdf("my-table.pdf")
styler.to_svg("my-table.svg")

Alternative Solutions

Most of the to_html kwargs readily apply to SVG so .svg export support could be incorporated into that method but should then be highlighted in the docs.

    @Substitution(buf=buffering_args, encoding=encoding_args)
    def to_html(
        self,
        buf: FilePath | WriteBuffer[str] | None = None,
        *,
        table_uuid: str | None = None,
        table_attributes: str | None = None,
        sparse_index: bool | None = None,
        sparse_columns: bool | None = None,
        bold_headers: bool = False,
        caption: str | None = None,
        max_rows: int | None = None,
        max_columns: int | None = None,
        encoding: str | None = None,
        doctype_html: bool = False,
        exclude_styles: bool = False,
        **kwargs,
    ) -> str | None:

Additional Context

No response

@janosh janosh added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 5, 2023
@janosh
Copy link
Contributor Author

janosh commented Apr 5, 2023

Slightly related (also talks about to_pdf): #26804

@attack68
Copy link
Contributor

attack68 commented Apr 6, 2023

I don't know SVG or PDF generation well but its strikes me that there are one or two things that are different here.

HTML, LateX and Excel have their own structured language for creating styles, be it some kind of attribute value pair. That allows a template language in python to easily generate tables. Some are easier than others. Latex has a bit of rewiring and excel a smaller bit of remapping, but they get the job done in a general and flexible way.

From what I read about SVG it seems to have a very general language meaning everything styled in a table here would have to be subjectively structured by the pandas developer - there seems to multiple ways of creating a basic table, for example, not to mention how one deals with styles such as font size, colors, borders, etc, which would probably have to manually coded?

@janosh
Copy link
Contributor Author

janosh commented Apr 6, 2023

From what I read about SVG it seems to have a very general language meaning everything styled in a table here would have to be subjectively structured by the pandas developer - there seems to multiple ways of creating a basic table, for example, not to mention how one deals with styles such as font size, colors, borders, etc, which would probably have to manually coded?

I don't follow. I.t.o. syntax and feature support, HTML and SVG are about as close as can be. In fact, you can embed HTML tables directly into an SVG using the <foreignObject> tag.

Here's an example of what that would look like table.svg:

<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
  <!-- Common use case: embed HTML text into SVG -->
  <foreignObject x="0" y="0" width="500" height="500">
    <!--
      In the context of SVG embedded in an HTML document, the XHTML
      namespace could be omitted, but it is mandatory in the
      context of an SVG document
    -->
    <div xmlns="http://www.w3.org/1999/xhtml">
      <table>
        <tr>
          <th>Company</th>
          <th>Contact</th>
          <th>Country</th>
        </tr>
        <tr>
          <td>Alfreds Futterkiste</td>
          <td>Maria Anders</td>
          <td>Germany</td>
        </tr>
        <tr>
          <td>Centro comercial Moctezuma</td>
          <td>Francisco Chang</td>
          <td>Mexico</td>
        </tr>
        <tr>
          <td>Ernst Handel</td>
          <td>Roland Mendel</td>
          <td>Austria</td>
        </tr>
        <tr>
          <td>Island Trading</td>
          <td>Helen Bennett</td>
          <td>UK</td>
        </tr>
        <tr>
          <td>Laughing Bacchus Winecellars</td>
          <td>Yoshi Tannamuri</td>
          <td>Canada</td>
        </tr>
        <tr>
          <td>Magazzini Alimentari Riuniti</td>
          <td>Giovanni Rovelli</td>
          <td>Italy</td>
        </tr>
      </table>
    </div>
  </foreignObject>
</svg>

@lithomas1 lithomas1 added Needs Discussion Requires discussion from core team before further action Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Discussion Requires discussion from core team before further action Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

3 participants