3
$\begingroup$

In my work environment I often find myself doing calculations on jupyter notebooks, then want to share the results over a MS word document. So much that I already asked how to convert jupyter notebooks to MS word documents (see: Practical way to convert jupyter notebook to MS Word document?). However, I am here for a slightly different question. Now I want to copy paste the html table output of a cell (typically a dataframe, or a display of a dataframe) to another MS word document (Mainly Word / Outlook / Teams). Is there any practical way to do so efficiently ? (Currently I am either capturing the screen or writing to a csv then open it to copy-paste).

$\endgroup$
3

4 Answers 4

2
$\begingroup$

Have you tried copy/pasting in two steps: HTML->Spreadsheet->Your other tool.

It seems that pasting into a spreadsheet works well from HTML and that many tools understand the clipboard format when copying from a spreadsheet.

For example, I often use this with Notebook->LibreOffice Calc->Gitlab, so that tables are automatically converted into markdown.

Edit: sorry, I see you have already discussed this in the comments, but I am still leaving the answer in case anyone else needs it.

$\endgroup$
2
$\begingroup$

Having the same problem (that's how I ended up here), and also seeing some other similar question, I got it working, even if I find it has too many steps:

Open the notebook in VS Code (with the Python extension you can open Jupyter notebooks there).

In the result cell, press the three dot option menu on its left, and select "Copy Output". This will copy the table in HTML format.

Save the output in HTML file.

Open the HTML file in your browser, select it, and copy it.

Paste with original formatting in word. It pastes as a table.

$\endgroup$
2
+100
$\begingroup$

There are a few practical ways to copy and paste HTML table output from a Jupyter Notebook into MS Word, Excel, Outlook, or Teams:

  • Using Python-docx:

  • Export to Excel or CSV: Export the HTML table to an Excel or CSV file and then import it into MS Word, Excel, or Outlook:

    Use pandas to create a DataFrame from your data. Use the df.to_excel() or df.to_csv() method to export the DataFrame to an Excel or CSV file.

    Open the Excel or CSV file, select the data, and copy-paste it into your MS Word, Excel, or Outlook document.

  • Using Markdown Table: If you prefer to use Markdown, you can convert the DataFrame to a Markdown table and then copy-paste it into your Microsoft application using pandas.DataFrame.to_markdown

# Assuming df is your DataFrame
print(df.to_markdown())
  • Using Pandas Styler: If you have customized styling applied to your DataFrame using Pandas Styler, you can use the render method to render the styled DataFrame as HTML, and then copy-paste it into your Microsoft application.
# Assuming df is your DataFrame with styling applied
html_output = df.style.render()
  • Export as Image: If none of the options work well for you, consider exporting the table as an image and inserting it into your MS Word, Excel, or Outlook document:

    Use a library like matplotlib or seaborn to visualize the DataFrame as a plot. Save the plot as an image file (e.g., PNG). Insert the image file into your MS Word, Excel, or Outlook document.

$\endgroup$
1
  • $\begingroup$ None of the method seems to create a MS table into word... $\endgroup$ Feb 18 at 10:56
1
$\begingroup$

Pandas has a way to return a dataframe as HTML (pandas.DataFrame.to_html): https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html

For ease, you could even add the pyperclip package (https://pypi.org/project/pyperclip/) to automatically copy the HTML to your clipboard

Something like this:

import pandas as pd
import pyperclip

# Sample DataFrame
df = pd.DataFrame({
    'Column1': [1, 2, 3],
    'Column2': ['A', 'B', 'C']
})

# Convert DataFrame to HTML
html_table = df.to_html(index=False)

# Copy HTML to clipboard
pyperclip.copy(html_table)
$\endgroup$
7
  • 1
    $\begingroup$ maybe I am missing something but the result of the paste is not a MS word / PPT / Outlook table. $\endgroup$ Feb 8 at 9:34
  • $\begingroup$ As far as I know, if you have an HTML table, you can just copy paste it. The style etc. might be a bit different. There are tools like pypi.org/project/df2img that allow you to export a dataframe table as an image $\endgroup$ Feb 8 at 9:50
  • 1
    $\begingroup$ Ah yes, I tried going directly into word... which did not work... isn't there a way to go directly from jupyter to WORD / PPT / OUTLOOK ? $\endgroup$ Feb 8 at 10:21
  • 1
    $\begingroup$ From what I found, going through excel first is often best: stackoverflow.com/questions/28714894/… You could then, export your dataframe directly into excel, and then copy it from there $\endgroup$ Feb 8 at 10:26
  • 2
    $\begingroup$ I can see some reasons for going torough Excel first (one of them being that it works) but I can't convince myself that it is the best solution. $\endgroup$ Feb 8 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.