Feature Type

  • [X] 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

Comment From: janosh

Slightly related (also talks about to_pdf): https://github.com/pandas-dev/pandas/issues/26804

Comment From: attack68

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?

Comment From: janosh

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>