0

I have written a Python notebook .ipynb in Visual Studio Code which includes code chunks and markdown chunks. I want to export this file into Microsoft Word .docx format. It is easy to export it as .pdf but how can I export it into .docx?

Any brief solution?

Thomas
  • 8,357
  • 15
  • 45
  • 81
  • Does this answer your question? [Convert Jupyter notebook into MS word document .doc?](https://stackoverflow.com/questions/53207734/convert-jupyter-notebook-into-ms-word-document-doc) – Thomas Aug 28 '23 at 23:14

3 Answers3

1

convert .ipynb to .md (markdown) and using pandoc your_input.md -o your_output.docx

nisakova
  • 89
  • 6
0

A workaround would be to download the file as a pdf, then open the pdf with Word and 'Save As' docx.

I_K
  • 1
  • 1
0

The easiest way is probably to use Pandoc. Simply type following command to your Command Prompt:

pandoc super-secret-notebook.ipynb -s -o export.docx

As @I_K suggested, you can export it to a PDF file and then save it as a Word document. Since you have the tag python in your question, I assume that you are looking for a Python solution.

Here is an example Python script doing this (requires nbconvert, pywin32, Pandoc and Microsoft Word installed):

import os
from pathlib import Path

import subprocess
import win32com.client


PATH_TO_NBCONVERT_EXE = r"C:\apps\python-3.11.1\Scripts\jupyter-nbconvert.exe"

cwd = Path(__file__).parent.resolve()

SCRIPT_PATH = os.path.join(cwd, "super-secret-notebook.ipynb")
FILE_PATH = os.path.join(cwd, "export.docx")

def main():

    subprocess.run(
        ["jupyter-nbconvert", "--execute", "--to", "pdf", SCRIPT_PATH], 
        cwd=cwd, 
        shell=True
    )

    word = win32com.client.Dispatch("Word.Application")

    doc = word.Documents.Add(SCRIPT_PATH.replace(".ipynb", ".pdf"))
    doc.SaveAs(FILE_PATH, FileFormat=12)
    doc.Close()

    word.Quit()

if __name__ == "__main__":
    main()

The output looks a bit different than simply using Pandoc:

enter image description here

Please note that the script above (and below) is executing nbconvert in the command prompt. You could also use the package nbconvert in Python itself.


Why do you want a Word document? PDF files are easier to distribute. Anyways, if you just need a PDF file, you can generate the file in your Command Prompt as well (without a script):

jupyter-nbconvert --execute --to pdf super-secret-notebook.ipynb

It still requires nbconvert and Pandoc installed.


You can also export to an HTML file and then save it as a Word document. (not requiring Pandoc)

Following Python script executes and exports a Notebook file to HTML and then saves it as a Word document. Formatting is not very useful though.

import os
from pathlib import Path

import subprocess
import win32com.client


PATH_TO_NBCONVERT_EXE = r"C:\apps\python-3.11.1\Scripts\jupyter-nbconvert.exe"

cwd = Path(__file__).parent.resolve()

SCRIPT_PATH = os.path.join(cwd, "super-secret-notebook.ipynb")
FILE_PATH = os.path.join(cwd, "export.docx")

def main():

    subprocess.run(
        ["jupyter-nbconvert", "--execute", "--to", "html", SCRIPT_PATH], 
        cwd=cwd, 
        shell=True
    )

    word = win32com.client.Dispatch("Word.Application")

    doc = word.Documents.Add(SCRIPT_PATH.replace(".ipynb", ".html"))
    doc.SaveAs(FILE_PATH, FileFormat=12)
    doc.Close()

    word.Quit()

if __name__ == "__main__":
    main()

enter image description here


Off-topic: Just some note if anyone comes across this answer and is wondering where to find the constants for the FileFormat parameter:

Thomas
  • 8,357
  • 15
  • 45
  • 81