Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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...? –  sebhofer May 13 '12 at 12:45
add comment

2 Answers 2

up vote 11 down vote accepted

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

nb=InputNotebook[];
fn=FileNameJoin[{"output.txt"}];
FrontEndExecute[FrontEndToken[nb,"Save",{fn,"Text"}]]

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

share|improve this answer
    
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. –  Szabolcs May 13 '12 at 12:28
add comment

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).

share|improve this answer
    
Does this really bypass the FE, i.e. it doesn't call it even in the background as it happens when exporting graphics? –  Szabolcs May 21 '12 at 15:00
    
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]:= –  ragfield May 22 '12 at 1:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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