How do I convert html tables in my notebook to PDF?

1,019 views
Skip to first unread message

Braun Brelin

unread,
Feb 18, 2017, 4:32:02 PM2/18/17
to Project Jupyter
Hi, 

I have a jupyter note book that has a number of cells with embedded HTML tables that look like this:

<table width='750px', align='left'>
<tr>
<td>
<p style='font-family:courier'>
some text here
</p>
</td>
</tr>
</table>

When I run jupyter nbconvert --to pdf mynotebook.ipynb it fails to render the HTML tables at all. 
I end up with text that looks like:
apache: # ID declaration pkg: # state declaration - installed # function declaration
Rather than a table with one element that has three spaced lines in it. 

I'm running this on an Ubuntu 14.04 system.  

Any help appreciated. 
 

Steve Spicklemire

unread,
Feb 18, 2017, 4:48:05 PM2/18/17
to jup...@googlegroups.com
I think markdown works for tables.

thanks,
-steve
--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/7f2f6a6f-923b-4df7-b62e-3af037773f57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Braun Brelin

unread,
Feb 19, 2017, 1:42:33 AM2/19/17
to jup...@googlegroups.com
Unfortunately, it doesn't make any difference.  It doesn't seem to recognize the <table> </table> element and just displays the table contents directly. 



On Sat, Feb 18, 2017 at 10:48 PM, Steve Spicklemire <stevespi...@gmail.com> wrote:
I think markdown works for tables.

thanks,
-steve

On Feb 18, 2017, at 4:32 PM, Braun Brelin <bbr...@gmail.com> wrote:

Hi, 

I have a jupyter note book that has a number of cells with embedded HTML tables that look like this:

<table width='750px', align='left'>
<tr>
<td>
<p style='font-family:courier'>
some text here
</p>
</td>
</tr>
</table>

When I run jupyter nbconvert --to pdf mynotebook.ipynb it fails to render the HTML tables at all. 
I end up with text that looks like:
apache: # ID declaration pkg: # state declaration - installed # function declaration
Rather than a table with one element that has three spaced lines in it. 

I'm running this on an Ubuntu 14.04 system.  

Any help appreciated. 
 

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/7f2f6a6f-923b-4df7-b62e-3af037773f57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/8DjjFR51V6s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Steve Spicklemire

unread,
Feb 19, 2017, 7:50:58 AM2/19/17
to jup...@googlegroups.com, Steve Spicklemire
Did you try using markdown rather than HTML?

| Colum 1 | Column 2 |
|------------------------------------|-----------------------------------|
| Thing 1 | Thing 2 |
| Thing 3 | Think 4 |

That works for me.

-steve
>> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
>> To post to this group, send email to jup...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/7f2f6a6f-923b-4df7-b62e-3af037773f57%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/8DjjFR51V6s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/B1968EBE-5CCD-4CB9-91FD-5391E58CC633%40gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/CAJ2Ovpgi1OyeNc%3D_4HF0ZD4woqMRRaZ3KPkfN%2B7CYOy1fxwsAQ%40mail.gmail.com.

Braun Brelin

unread,
Feb 19, 2017, 1:21:43 PM2/19/17
to jup...@googlegroups.com
Yes, but I've got lots and lots of these tables in lots of cells.  I don't really want to have to manually convert my html tables to markdown format.  Besides, isn't markdown supposed to be a superset of HTML?  



>> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.

>> To post to this group, send email to jup...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/7f2f6a6f-923b-4df7-b62e-3af037773f57%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/8DjjFR51V6s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.

> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/B1968EBE-5CCD-4CB9-91FD-5391E58CC633%40gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.

> To post to this group, send email to jup...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/8DjjFR51V6s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Damon Allen

unread,
Feb 20, 2017, 6:47:08 AM2/20/17
to Project Jupyter

Besides, isn't markdown supposed to be a superset of HTML?  

You can think of markdown as a subset of HTML.  You won't be able to use the specify the width, like in your example and if you have merged cells you won't be able to duplicate it using Markdown.  There was some initial work on a library that would support Latex and HTML tables a couple years back (https://github.com/takluyver/tabipy) but it's a complicated process trying to support two separate encodings with all the different approaches.  The automatic conversion of HTML tables to Latex is something I've wanted to be added to nbconvert for a long time but I've never found the time to write a converter.

With regards to converting HTML tables to Markdown, if you're tables are simple enough to use Markdown then you can use a Python script to partially automate the conversion.

In a new cell copy the HTML copy for one of your tables and assign it to the variable txt in the code below and you should be able to create a Markdown table equivalent.  Admittedly this code only works with clean, simple HTML tables.  Also, if you are not familiar with regular expressions and would like to try to improve the code you can check out http://regexr.com/.

import re

txt = """your table code
more of your table code
"""

rows = re.compile(r'(?:<tr>)(.?)(?:</tr>)')
headers = re.compile(r'(?:<th>)(.*?)(?:</th>)')
cells = re.compile(r'(?:<td>)(.?)(?:</td>)')

table_data = []
table_text = txt.replace('\n','')
for row in rows.findall(table_text):
    row_content = headers.findall(row)
    if len(row_content)==0:
       
row_content = cells.findall(row)
    table_data.append(row_content)

markdown_text = ''
for index, row in enumerate(table_data):
    markdown_text += '|'+'|'.join(row)+'|\n'
    if index == 0:
        markdown_text+='|'+'|'.join(['----']*len(row))+'|\n'
print(markdown_text)

Tony Hirst

unread,
Feb 22, 2017, 7:09:54 AM2/22/17
to Project Jupyter
I started exploring using pytablewriter ( https://github.com/thombashi/pytablewriter ) to generate markdown tables, but ended up instead opting for pandas .to_html() to generate tabular output for an output slidedeck.

--tony
>> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

>> To post to this group, send email to jup...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/7f2f6a6f-923b-4df7-b62e-3af037773f57%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/8DjjFR51V6s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jupyter+u...@googlegroups.com.

> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/B1968EBE-5CCD-4CB9-91FD-5391E58CC633%40gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

> To post to this group, send email to jup...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/8DjjFR51V6s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+u...@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages