skip to Main Content

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?

3

Answers


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

    Login or Signup to reply.
  2. 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:appspython-3.11.1Scriptsjupyter-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()
    

    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:appspython-3.11.1Scriptsjupyter-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()
    

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

    Login or Signup to reply.
  3. convert .ipynb to .md (markdown) and using pandoc your_input.md -o your_output.docx

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search