---
title: "NotebookImport"
language: "en"
type: "Symbol"
summary: "NotebookImport[notebook, style] imports cells with the given cell style from the specified notebook. NotebookImport[notebook, style -> form] imports cells in the form specified by form."
keywords: 
- scrape notebook
- scraping
- notebook scraping
- notebook extraction
- cell extraction
- extract data from a notebook
- plaintext from notebook
- inputs from notebook
- import inputs
- scrape inputs
- scrape text
canonical_url: "https://reference.wolfram.com/language/ref/NotebookImport.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Importing and Exporting"
    link: "https://reference.wolfram.com/language/guide/ImportingAndExporting.en.md"
  - 
    title: "Notebook Document Generation"
    link: "https://reference.wolfram.com/language/guide/DocumentGeneration.en.md"
  - 
    title: "Automated Reports"
    link: "https://reference.wolfram.com/language/guide/AutomatedReports.en.md"
  - 
    title: "Text Manipulation"
    link: "https://reference.wolfram.com/language/guide/ProcessingTextualData.en.md"
  - 
    title: "Low-Level Notebook Programming"
    link: "https://reference.wolfram.com/language/guide/LowLevelNotebookProgramming.en.md"
related_workflows: 
  - 
    title: "Import Material from a Notebook"
    link: "https://reference.wolfram.com/language/workflow/ImportMaterialFromANotebook.en.md"
  - 
    title: "Import Tabular Data from a Notebook"
    link: "https://reference.wolfram.com/language/workflow/ImportTabularDataFromANotebook.en.md"
  - 
    title: "Run a Notebook Programmatically"
    link: "https://reference.wolfram.com/language/workflow/RunANotebookProgrammatically.en.md"
related_functions: 
  - 
    title: "Import"
    link: "https://reference.wolfram.com/language/ref/Import.en.md"
  - 
    title: "NotebookGet"
    link: "https://reference.wolfram.com/language/ref/NotebookGet.en.md"
  - 
    title: "NotebookRead"
    link: "https://reference.wolfram.com/language/ref/NotebookRead.en.md"
  - 
    title: "NotebookEvaluate"
    link: "https://reference.wolfram.com/language/ref/NotebookEvaluate.en.md"
  - 
    title: "Cells"
    link: "https://reference.wolfram.com/language/ref/Cells.en.md"
  - 
    title: "ReadList"
    link: "https://reference.wolfram.com/language/ref/ReadList.en.md"
  - 
    title: "MakeExpression"
    link: "https://reference.wolfram.com/language/ref/MakeExpression.en.md"
  - 
    title: "SemanticImport"
    link: "https://reference.wolfram.com/language/ref/SemanticImport.en.md"
  - 
    title: "NotebookTemplate"
    link: "https://reference.wolfram.com/language/ref/NotebookTemplate.en.md"
related_tutorials: 
  - 
    title: "Cells as Wolfram Language Expressions"
    link: "https://reference.wolfram.com/language/tutorial/ManipulatingNotebooks.en.md#24199"
  - 
    title: "Notebooks as Wolfram Language Expressions"
    link: "https://reference.wolfram.com/language/tutorial/ManipulatingNotebooks.en.md#31306"
---
# NotebookImport

NotebookImport[notebook, style] imports cells with the given cell style from the specified notebook.

NotebookImport[notebook, style -> form] imports cells in the form specified by form.

## Details and Options

* In ``NotebookImport[notebook, …]``, ``notebook`` can be a ``NotebookObject``, a file name, a ``CloudObject`` corresponding to a notebook, or a ``Notebook`` expression.

* If style is a pattern, cells whose style matches that pattern are imported. A typical pattern is "Subscript[name, 1]"\|"Subscript[name, 2]"\|\[Ellipsis], indicating to import any of the styles Subscript[name, i].

* If ``style`` is ``_``, all cells are imported.

* Possible values for ``form`` include:

|                       |                                                     |
| --------------------- | --------------------------------------------------- |
| "Boxes"               | first argument of low-level cell expression         |
| "Cell"                | complete low-level cell expression                  |
| "Expression"          | cell converted to an expression                     |
| "HeldExpression"      | cell converted to a held expression                 |
| "HeldInterpretedCell" | cell converted to a held TextCell or ExpressionCell |
| "Hyperlinks"          | list of hyperlinks in the cell                      |
| "InactiveExpression"  | cell converted to an inactive expression            |
| "InputText"           | cell contents as given by Copy As ▶ Input Text      |
| "InterpretedCell"     | cell converted to a TextCell or ExpressionCell      |
| "Text"                | cell contents as given by Copy As ▶ Plain Text      |

* The following options can be given:

|                       |           |                                                                         |
| --------------------- | --------- | ----------------------------------------------------------------------- |
| StripCellGrouping     | True      | whether to strip nested lists corresponding to the cell group hierarchy |
| CellStyleImportRules  | Automatic | specify forms to return for different cell styles                       |

* With the setting ``StripCellGrouping -> False``, one level of the list will be present for every cell group in the notebook, whether or not any cells were imported from that group.

* The setting ``CellStyleImportRules -> Automatic`` is equivalent to the following mappings:

|                 |                  |
| --------------- | ---------------- |
| "Title"         | "Text"           |
| "Chapter"       | "Text"           |
| "Subchapter"    | "Text"           |
| "Section"       | "Text"           |
| "Subsection"    | "Text"           |
| "Subsubsection" | "Text"           |
| "Text"          | "Text"           |
| "Code"          | "Cell"           |
| "Input"         | "HeldExpression" |
| "Output"        | "Expression"     |
| "Message"       | "Text"           |
| "Print"         | "Print"          |

* Other styles are given in ``"Boxes"`` form.

---

## Examples (15)

### Basic Examples (1)

Create a notebook:

```wl
In[1]:=
nb = CreateDocument[{TextCell["project title", "Title"], TextCell["first section", "Section"], TextCell["details", "Subsection"], ExpressionCell[Defer[2 + 2], "Input"], TextCell["second section", "Section"], ExpressionCell[Defer[3 + 3], "Input"], ExpressionCell[Defer[1 / 0], "Input"]}, WindowMargins -> {{Automatic, 0}, {Automatic, 0}}, WindowSize -> {500, 600}];
NotebookEvaluate[nb, InsertResults -> True];
```

Import the cells in this notebook to the default forms:

```wl
In[2]:= NotebookImport[nb, _]

Out[2]= {"project title", "first section", "details", HoldComplete[2 + 2], 4, "second section", HoldComplete[3 + 3], 6, HoldComplete[(1/0)], "Power::infy: Infinite expression 1/0 encountered.", ComplexInfinity}
```

Import cells in this notebook as a list of text strings:

```wl
In[3]:= NotebookImport[nb, _ -> "Text"]

Out[3]= {"project title", "first section", "details", "2+2", "4", "second section", "3+3", "6", "1/0", "Power::infy: Infinite expression 1/0 encountered.", "ComplexInfinity"}
```

Import only Input-style cells, as held expressions:

```wl
In[4]:= NotebookImport[nb, "Input"]

Out[4]= {HoldComplete[2 + 2], HoldComplete[3 + 3], HoldComplete[(1/0)]}
```

Import input cells as evaluatable text:

```wl
In[5]:= NotebookImport[nb, "Input" -> "InputText"]

Out[5]= {"2 + 2", "3 + 3", "1/0"}
```

Import input cells as inactive expressions:

```wl
In[6]:= NotebookImport[nb, "Input" -> "InactiveExpression"]

Out[6]= {2 + 2, 3 + 3, 1 * 0 ^ (-1)}
```

Import all input and output cells from a notebook, preserving cell grouping information:

```wl
In[7]:= NotebookImport[nb, "Input" | "Output", StripCellGrouping -> False]

Out[7]= {{{{{HoldComplete[2 + 2], 4}}}, {{HoldComplete[3 + 3], 6}, {HoldComplete[(1/0)], ComplexInfinity}}}}
```

Import evaluatable text for every input and output cell:

```wl
In[8]:= NotebookImport[nb, "Input" | "Output" -> "InputText", StripCellGrouping -> False]

Out[8]= {{{{{"2 + 2", "4"}}}, {{"3 + 3", "6"}, {"1/0", "ComplexInfinity"}}}}
```

### Scope (6)

Import from a notebook file:

```wl
In[1]:= NotebookImport[FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", "ExampleData", "document.nb"}], "Section" -> "Text"]

Out[1]= {"Text", "Expressions", "Graphics", "Dynamic Objects"}
```

---

Import from a ``Notebook`` expression:

```wl
In[1]:= NotebookImport[Notebook[{Cell[BoxData[RowBox[{"2", "+", "2"}]], "Input"]}], _ -> "InactiveExpression"]

Out[1]= {2 + 2}
```

---

The ``"InterpretedCell"`` form results in a list of ``TextCell`` and ``ExpressionCell`` expressions, as appropriate:

```wl
In[1]:= NotebookImport[Notebook[{Cell[BoxData[RowBox[{"2", "+", "2"}]], "Input"]}], _ -> "InterpretedCell"]//InputForm
```

Out[1]//InputForm= {ExpressionCell[4, "Input"]}

The ``"HeldInterpretedCell"`` form returns held expressions:

```wl
In[2]:= NotebookImport[Notebook[{Cell[BoxData[RowBox[{"2", "+", "2"}]], "Input"]}], _ -> "HeldInterpretedCell"]//InputForm
```

Out[2]//InputForm= {HoldComplete[ExpressionCell[2 + 2, "Input"]]}

---

The ``"Cell"`` form returns expressions appropriate for use in low-level notebook operations, like ``NotebookPut`` :

```wl
In[1]:= NotebookPut @ Notebook @NotebookImport[EvaluationNotebook[], "ObjectName" | "Notes" -> "Cell"];
```

---

The ``"Hyperlinks"`` form returns a list of ``Hyperlink`` expressions imported from each cell:

```wl
In[1]:= NotebookImport[EvaluationNotebook[], "Notes" -> "Hyperlinks"]

Out[1]= {{[NotebookObject](NotebookObject), [CloudObject](CloudObject), [Notebook](Notebook)}, {}, {}, {}, {}, {[StripCellGroups](StripCellGroups), [False](False)}, {[CellStyleImportRules](CellStyleImportRules), [Automatic](Automatic)}, {}}
```

---

Specify a custom importer to extract ``StyleBox`` structures from ``"Notes"``-style cells:

```wl
In[1]:= NotebookImport[EvaluationNotebook[], "Notes"  -> (Cases[#2, _StyleBox, Infinity]&)]

Out[1]= {{StyleBox["notebook", "TI"], StyleBox["…", "TR"], StyleBox["notebook", "TI"]}, {StyleBox["style", "TI"], StyleBox["…", "TR"], StyleBox["name", "TI"], StyleBox["i", "TI"]}, {StyleBox["style", "TI"]}, {StyleBox["form", "TI"]}, {}, {}, {}, {}}
```

### Options (4)

#### CellStyleImportRules (2)

Specify different forms for different cell styles:

```wl
In[1]:= NotebookImport[FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", "ExampleData", "document.nb"}], "Text" | "Input", CellStyleImportRules -> {"Text" -> "Text", "Input" -> "InputText"}]

Out[1]= {"A line of text.", "one two three", "alpha beta gamma delta epsilon …", "2 + 2", "N[GoldenRatio, 80]", "Nest[Style[{#, y}, FontColor -> Hue[Depth[#]/18, 1, .5]] &, x, 6]", "Graphics3D[Sphere[], ImageSize -> Tiny]", "Plot[Abs[x], {x, -2, 2}, ImageSize -> Small, AspectRatio -> Automatic]", "Slider[Dynamic[x]]", "Dynamic[x]"}
```

---

Different cell styles can specify different custom importers:

```wl
In[1]:= NotebookImport[EvaluationNotebook[], "Input" | "Output" | {"Message", "MSG"}, CellStyleImportRules -> {{"Message", "MSG"} -> (#2&), _ -> (#1 -> ByteCount[#2]&)}]

Out[1]= {"Input" -> 9408, "Input" -> 1424, "Output" -> 2856, "Input" -> 1224, "Output" -> 1872, "Input" -> 1224, "Output" -> 2120, "Input" -> 1408, "Output" -> 1008, "Input" -> 1176, "Output" -> 3208, "Input" -> 2080, "Output" -> 3984, "Input" -> 2592, "Ou ...  6224, "Input" -> 2088, "Output" -> 18480, "Input" -> 1984, "Output" -> 2120, "Input" -> 808, "Output" -> 576, "Input" -> 1976, "Output" -> 912, "Input" -> 2216, "Output" -> 1120, "Input" -> 2456, "Output" -> 952, "Input" -> 992, "Output" -> 1752}
```

#### StripCellGrouping (2)

Import a nested textual representation of some of the cells in a notebook:

```wl
In[1]:= NotebookImport[FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", "ExampleData", "document.nb"}], "Subsection" | "Text" -> "Text", StripCellGrouping -> False]

Out[1]= {{"A line of text.", {"Subsection", "one two three", "alpha beta gamma delta epsilon …"}}, {{}, {}, {}}, {{}, {}}, {{}, {}}}
```

---

Import a nested textual representation of all the cells in a notebook:

```wl
In[1]:= NotebookImport[FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", "ExampleData", "document.nb"}], _ -> "Text", StripCellGrouping -> False]

Out[1]= {{"Text", "A line of text.", {"Subsection", "one two three", "alpha beta gamma delta epsilon …"}}, {"Expressions", {"2+2", "4"}, {"N[GoldenRatio,80]", "1.6180339887498948482045868343656381177203091798057628621354486227052604628189024"}, {"Nest[Styl ... ,y},y},y}"}}, {"Graphics", {"Graphics3D[Sphere[],ImageSize->Tiny]", ""}, {"Plot[Abs[x],{x,-2,2},ImageSize->Small,AspectRatio->Automatic]", ""}}, {"Dynamic Objects", {"Slider[Dynamic[x]]", SliderBox[Dynamic[$CellContext`x]]}, {"Dynamic[x]", "0."}}}
```

### Possible Issues (2)

Cells that contain an incomplete expression will fail when imported to an expression-based form:

```wl
In[1]:= NotebookImport[Notebook[{Cell["2+", "Input"]}], "Input" -> "HeldExpression"]

Out[1]=
{Failure["InterpretationFailure", Association["CellStyle" -> "Input", "ContentData" -> TextData, 
  "Cell" -> RawBoxes[Cell["2+", "Input"]]]]}
```

---

``HoldComplete`` affects only evaluation, not typesetting:

```wl
In[1]:= NotebookImport[Notebook[{Cell["Grid[{{1,2},{3,4}}]", "Input"]}], "Input" -> "HeldExpression"]

Out[1]=
{HoldComplete[|   |   |
| - | - |
| 1 | 2 |
| 3 | 4 |]}
```

Use ``InputForm`` to see the untypeset form of the returned expressions:

```wl
In[2]:= InputForm[%]
```

Out[2]//InputForm= {HoldComplete[Grid[{{1, 2}, {3, 4}}]]}

String-based forms are inert to evaluation and most typesetting:

```wl
In[3]:= NotebookImport[Notebook[{Cell["Grid[{{1,2},{3,4}}]", "Input"]}], "Input" -> "InputText"]

Out[3]= {"Grid[{{1,2},{3,4}}]"}
```

### Neat Examples (2)

Import historical information from a function's reference page:

```wl
In[1]:= docsDirectory = SystemInformation["FrontEnd", "DocumentationInformation"]["Directory"];StringTrim[NotebookImport[FileNameJoin[{docsDirectory, "English", "System", "ReferencePages", "Symbols", "NDSolve.nb"}], "HistorySection" -> "Text"]]

Out[1]=
{"History\r
Introduced in 1991 (2.0) | Updated in 1996 (3.0) ▪ 2003 (5.0) ▪ 2007 (6.0) ▪ 2008 (7.0) ▪ 2012 (9.0) ▪ 2014 (10.0) ▪ 2019 (12.0)"}
```

---

Import the list of symbol names in the See Also links of a reference page:

```wl
In[1]:=
docsDirectory = SystemInformation["FrontEnd", "DocumentationInformation"]["Directory"];
strings = StringTrim[NotebookImport[FileNameJoin[{docsDirectory, "English", "System", "ReferencePages", "Symbols", "NDSolve.nb"}], "SeeAlsoSection" -> "Text"]]

- Out[1]=
{"See Also\r
DSolve
- NDSolveValue
- ParametricNDSolve
- AsymptoticDSolveValue
- NDEigensystem
- NDEigenvalues
- NIntegrate
- NSolve
- DifferentialRoot
- StreamPlot
- SystemModelSimulate
- ItoProcess"}

- In[2]:=
StringSplit[strings, "
- " | "
"]

Out[2]= {{"See Also\r", "DSolve", "NDSolveValue", "ParametricNDSolve", "AsymptoticDSolveValue", "NDEigensystem", "NDEigenvalues", "NIntegrate", "NSolve", "DifferentialRoot", "StreamPlot", "SystemModelSimulate", "ItoProcess"}}
```

## See Also

* [`Import`](https://reference.wolfram.com/language/ref/Import.en.md)
* [`NotebookGet`](https://reference.wolfram.com/language/ref/NotebookGet.en.md)
* [`NotebookRead`](https://reference.wolfram.com/language/ref/NotebookRead.en.md)
* [`NotebookEvaluate`](https://reference.wolfram.com/language/ref/NotebookEvaluate.en.md)
* [`Cells`](https://reference.wolfram.com/language/ref/Cells.en.md)
* [`ReadList`](https://reference.wolfram.com/language/ref/ReadList.en.md)
* [`MakeExpression`](https://reference.wolfram.com/language/ref/MakeExpression.en.md)
* [`SemanticImport`](https://reference.wolfram.com/language/ref/SemanticImport.en.md)
* [`NotebookTemplate`](https://reference.wolfram.com/language/ref/NotebookTemplate.en.md)

## Tech Notes

* [Cells as Wolfram Language Expressions](https://reference.wolfram.com/language/tutorial/ManipulatingNotebooks.en.md#24199)
* [Notebooks as Wolfram Language Expressions](https://reference.wolfram.com/language/tutorial/ManipulatingNotebooks.en.md#31306)

## Related Guides

* [Importing and Exporting](https://reference.wolfram.com/language/guide/ImportingAndExporting.en.md)
* [Notebook Document Generation](https://reference.wolfram.com/language/guide/DocumentGeneration.en.md)
* [Automated Reports](https://reference.wolfram.com/language/guide/AutomatedReports.en.md)
* [Text Manipulation](https://reference.wolfram.com/language/guide/ProcessingTextualData.en.md)
* [Low-Level Notebook Programming](https://reference.wolfram.com/language/guide/LowLevelNotebookProgramming.en.md)

## Related Workflows

* [Import Material from a Notebook](https://reference.wolfram.com/language/workflow/ImportMaterialFromANotebook.en.md)
* [Import Tabular Data from a Notebook](https://reference.wolfram.com/language/workflow/ImportTabularDataFromANotebook.en.md)
* [Run a Notebook Programmatically](https://reference.wolfram.com/language/workflow/RunANotebookProgrammatically.en.md)

## History

* [Introduced in 2015 (10.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn101.en.md) \| [Updated in 2024 (14.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn140.en.md)