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

Customizing Table style in Word Output via Quarto #10066

Open
hasibagen opened this issue Jun 20, 2024 · 7 comments
Open

Customizing Table style in Word Output via Quarto #10066

hasibagen opened this issue Jun 20, 2024 · 7 comments
Labels
docx Issues with the docx format enhancement New feature or request pandoc themes Related to HTML theming or any other style related issue (like highlight-style)
Milestone

Comments

@hasibagen
Copy link

hasibagen commented Jun 20, 2024

Bug description

I encountered an issue where a table generated with pd.to_markdown in Jupyter Lab loses its borders when exported to Word using Quarto. The borders appear correctly in the PDF output.

How can I resolve the issue of missing table borders in the Word output?
How can I adjust the table style, such as center alignment, when generating Word documents? I am aware of the capabilities of knitr in R Markdown but need a solution entirely in Python.

Thank you for your assistance.

Steps to reproduce

import seaborn as sns
from IPython.display import Markdown, display
mpg = sns.load_dataset("mpg").head(3)
display(Markdown(mpg.to_markdown(index = False)))

Default Left Right Center
12 12 12 12
123 123 123 123
1 1 1 1

: Demonstration of pipe table syntax

bug_demo.zip

Expected behavior

Expected: The table should have borders in both PDF and Word outputs.

Actual behavior

Actual: The table has borders in the PDF output but lacks borders in the Word output.

Your environment

Operating System: Ubuntu 22.04
Jupyter Lab Version:
IPython : 8.18.1
ipykernel : 6.29.4
ipywidgets : 8.1.2
jupyter_client : 8.6.1
jupyter_core : 5.7.2
jupyter_server : 2.14.0
jupyterlab : 4.1.8
nbclient : 0.10.0
nbconvert : 7.16.4
nbformat : 5.10.4
notebook : 7.1.3
qtconsole : 5.5.2
traitlets : 5.14.3

Quarto check output

Quarto 1.5.45
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.2.0: OK
      Dart Sass version 1.70.0: OK
      Deno version 1.41.0: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.5.45
      Path: /opt/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: v2024.06
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Using: TinyTex
      Path: /home/jade/.TinyTeX/bin/x86_64-linux
      Version: 2024

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.9.6 (Conda)
      Path: /home/jade/anaconda3/envs/fnirs/bin/python
      Jupyter: 5.7.2
      Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
      Version: 4.4.0
      Path: /usr/lib/R
      LibPaths:
        - /home/jade/R/x86_64-pc-linux-gnu-library/4.4
        - /usr/local/lib/R/site-library
        - /usr/lib/R/site-library
        - /usr/lib/R/library
      knitr: 1.46
      rmarkdown: 2.26

[✓] Checking Knitr engine render......OK
@hasibagen hasibagen added the bug Something isn't working label Jun 20, 2024
@cderv
Copy link
Collaborator

cderv commented Jun 20, 2024

The docx you shared has some borders
image

PDF has more
image

But this is due to which Table is used in word document and how it is configured. You can change the default, but changing the style using a reference document.
https://quarto.org/docs/output-formats/ms-word-templates.html

I see that Style in your docx you shared
image

Hope it helps

@mcanouil mcanouil added docx Issues with the docx format themes Related to HTML theming or any other style related issue (like highlight-style) labels Jun 20, 2024
@hasibagen
Copy link
Author

The docx you shared has some borders image

PDF has more image

But this is due to which Table is used in word document and how it is configured. You can change the default, but changing the style using a reference document. https://quarto.org/docs/output-formats/ms-word-templates.html

I see that Style in your docx you shared image

Hope it helps

Thank you very much for your suggestion. I have modified the table style in the word template as you said, using libreoffice. However, after using the template to generate word, the exported word table is still dotted.
I would like to ask why this is? I also tried to use word and wps to modify the word template, but it seems that the result is the same, there is no table border.
This is my modified code and word template.
Looking forward to your reply.

with_word_temple.zip

@cderv
Copy link
Collaborator

cderv commented Jun 21, 2024

A few things:

  • You need to provide a docx file as reference doc. I don't know what LibreOffice is creating, but I believe Pandoc expect a recent docx format document, same as what it is producing. The document in the zip you provided do not seem to be latest docx format as when I opened an modify it in words, it will ask me for compatibility checker which means older version to newer.

  • I don't know how you modified styling, so let me be more precise.

    • The style of the table in the doc is the one to change by selecting another. By modifying styling, it means modifying the Default style that Pandoc will use for Tables.
      image

    See what we explain for other styles in our doc: https://quarto.org/docs/output-formats/ms-word-templates.html#sec-docx-create-template

    • You need to generate a doc with default styling (no reference doc) and modify the style of the table which by default seems to be the one called "Table"
      image

It works for me when I do this (though I am on Windows with Word to use docx files)

Changing default Table styles in something that Pandoc still needs to support for Quarto to be able to benefit from it

So for now, one need to follow exactly how reference doc template works with Pandoc to be able to customize a Quarto docx output

Hope it helps

@cderv cderv added support a request for support and removed bug Something isn't working labels Jun 21, 2024
@cderv cderv added this to the Future milestone Jun 21, 2024
@cderv cderv changed the title Issue with pd.to_markdown Table Borders Missing in Word Output via Quarto Customizing Table style in Word Output via Quarto Jun 21, 2024
@cderv cderv added the pandoc label Jun 21, 2024
@R3myG
Copy link

R3myG commented Oct 31, 2024

Will Quarto support this new functionality from Pandoc jgm/pandoc#10009 ?

I couldn't get it to work using the Quarto Release Candidate 1.6.32 which uses Pandoc 3.4.

@cderv
Copy link
Collaborator

cderv commented Oct 31, 2024

I missed this change by Pandoc, thanks for pointing it out.

I believe the PR you linked to is mainly aimed at HTML to docx conversion by modifying the HTML table writer only. If you look at on of the comment in there, you'll see that when editing with Markdown (like in Quarto), there is no easy way to pass attributes. I don't think Pandoc's Markdown reader as a way to read attributes in Grid table.

@tarleb can you confirm that passing attributes on Markdown table for Pandoc is not already there ?
Is this part of Markdown spec ?

So the syntax you tried is not seeing : {custom-style="TableCustom"} as attributes

❯ pandoc -t native
| Header 1 | Header 2 |
|----------|----------|
| Val1         |    Val2      |
: {custom-style="TableCustom"}
^Z
[ Table
    ( "" , [] , [] )
    (Caption
       Nothing
       [ Plain
           [ Str "{custom-style="
           , Quoted DoubleQuote [ Str "TableCustom" ]
           , Str "}"
           ]
       ])
    [ ( AlignDefault , ColWidthDefault )
    , ( AlignDefault , ColWidthDefault )
    ]
    (TableHead
(....TRUNCATED....)

This is seen as caption by Pandoc.

This syntax is Quarto specific and used for various Quarto only feature
https://quarto.org/docs/authoring/tables.html#using-bootstrap-classes

We do parse specific Table Caption for such content we know. THis happens in our Lua chain
https://github.com/quarto-dev/quarto-cli/blob/a36a7bd05e11309c9b2dd86746f10e89f676ce6c/src/resources/filters/common/tables.lua#L19-L55

So to support such addition for the Docx writers, Pandoc would first need to support passing attributes to markdown table. Quarto could do it though as it already does handle some attributes, but it is a feature request to support this correctly.

So for now, this is not working in Quarto (nor Pandoc when working with .md inputs)

@R3myG
Copy link

R3myG commented Oct 31, 2024

@cderv Thank you so much for taking the time to reply with such detailed information!

I've already used the trick of a reference docx and modified the default "Table" style.
I now have two different types of tables that would each require their own style, and from what I gather, it's currently not possible at all to do that.

@mcanouil mcanouil added enhancement New feature or request and removed support a request for support labels Oct 31, 2024
@tarleb
Copy link
Collaborator

tarleb commented Oct 31, 2024

I can confirm that pandoc Markdown, as of now, doesn't offer a way to set table attributes. I've commented in jgm/pandoc#10009 with a possible work-around that uses an extra Lua filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docx Issues with the docx format enhancement New feature or request pandoc themes Related to HTML theming or any other style related issue (like highlight-style)
Projects
None yet
Development

No branches or pull requests

5 participants