It is possible to export a notebook to plain text format (practically just extracting all the text from it and discarding formatting) using the File -> Save As... menu item.

How can a notebook be exported to plain text programmatically, without making use of any GUI?

With all the other document formats, such as PDF, HTML, NB, LaTeX, etc. it is possible to simply use Export:

ExportString[NotebookGet@EvaluationNotebook[], "LaTeX"]

This does not work with the export format "Text" though. It simply exports the notebook expression in a Mathematica expression format. Is there an export format which will export the contents of the notebook to plain text instead of the expression representing it?

I would prefer a method that can produce an in-memory string as well (just like ExportString), and can avoid writing on disk.

EDIT: Based on this answer, I found First@FrontEndExecute[ FrontEnd`ExportPacket[NotebookGet@InputNotebook[], "InputText"]]. I'd prefer something documented though, if it exists.

  • $\begingroup$ You probably know that you can get the notebook contents with NotebookGet and alike functions. Of course you'd still have to extract the relevant data from the Cell expressions. Maybe Leonid's code formating package can give pointers how to do that...? $\endgroup$
    – sebhofer
    Commented May 13, 2012 at 12:45

3 Answers 3


Saving is done by the front end, which you can exploit programatically by using FrontEndExecute. I think this is what you need:


Edit: Of course you can also save it as a package by replacing "Text" with "Package".

  • $\begingroup$ Thanks! I didn't realize it was possible to avoid a save dialog popping up. I'd still like to have a solution that can export to a string though. I updated my question with this. Many Export operations are done by the front end under the hood, and are implemented in terms of ExportPacket. I guess it would be possible to use ExportPacket (I haven't yet tried), but I'd prefer something documented. $\endgroup$
    – Szabolcs
    Commented May 13, 2012 at 12:28
  • 1
    $\begingroup$ Is there a way to set the max length of lines manually instead of it defaulting to 800 or so? $\endgroup$
    – William
    Commented Sep 3, 2015 at 5:58

If you want to completely bypass the FE you can use:

Import["your.nb", "Plaintext"]

This will return a textual representation of the notebook file in the form of a String expression, which you can then post-process or export however you see fit.

The results are not perfect, but they are decent. This is the mechanism used by the Spotlight plugin on Mac, the Google Desktop and other plugins on Windows, and the comparable mechanism on Linux (I forget the name).

  • $\begingroup$ Does this really bypass the FE, i.e. it doesn't call it even in the background as it happens when exporting graphics? $\endgroup$
    – Szabolcs
    Commented May 21, 2012 at 15:00
  • 1
    $\begingroup$ It does bypass the FE. To verify: $ /Applications/Mathematica.app/Contents/MacOS/MathKernel Mathematica 8.0 for Mac OS X x86 (64-bit) Copyright 1988-2010 Wolfram Research, Inc. In[1]:= Links[] Out[1]= {} In[2]:= Import["2.nb", "Plaintext"]; In[3]:= Links[] Out[3]= {LinkObject[/Applications/Mathematica.app/SystemFiles/Converters/Bina\ > ries/MacOSX-x86-64/NBImport.exe, 1, 1]} In[4]:= $\endgroup$
    – ragfield
    Commented May 22, 2012 at 1:40

Starting with version 10.1 you can also use NotebookImport to scrape the text from a notebook:



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.