top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Merge Like a Pro: 6 Easy Ways to Combine Jupyter Notebooks

May 24, 2025
3 min read

Updated: May 26, 2025

Here are several methods to merge multiple Jupyter notebooks into one, depending on whether you prefer manual, command-line, or scripted approaches:



1. Manual Merge (Easy, for Small Tasks)

Manual merging involves opening multiple notebooks, selecting the desired cells, and copying them into a single notebook. This approach is especially useful for quick edits, combining short notebooks, or customizing the structure and order of your content.


Steps:

  1. Open both notebooks in your Jupyter Notebook or JupyterLab environment.

  2. Select the cells you want to transfer (use Shift + Click for multiple).

  3. Right-click and select Copy Cells or use keyboard shortcuts.

  4. Paste the cells into the target notebook.


Best For:

  • Small projects or quick edits

  • Combining a few notebooks

  • Beginners or non-programmers

Not Recommended For:

  • Large-scale projects with many notebooks

  • Automating repetitive tasks

  • Merging notebooks regularly as part of a workflow



2. Using nbmerge (Command-Line Tool)

nbmerge is a lightweight Python-based tool that lets you combine multiple Jupyter notebooks into a single .ipynb file. This is perfect for merging several notebooks quickly and consistently using the command line.


It reads multiple .ipynb files and appends their content (cells) in order into a single notebook file. It only combines the code and markdown cells, not the outputs, which keeps the result clean and lightweight.


Steps:

  1. Install nbmerge.

    pip install nbmerge

    This will add the nbmerge command to your system, which you can run from the terminal or command prompt.

  2. Create or Prepare Notebooks

    Make sure your notebooks are saved and organized in the same directory or note the full paths.

    Example:

project/ 
├── intro.ipynb
├── data_analysis.ipynb
├── model_training.ipynb
  1. Merge Notebooks Using the Command Line

    Use the following command:


nbmerge intro.ipynb data_analysis.ipynb model_training.ipynb > merged_notebook.ipynb


This does the following:

  • Reads each notebook in the order listed

  • Extracts their cells

  • Merges all cells into a new file called merged_notebook.ipynb


Best For:

  • Automating notebook merging in a workflow

  • Quickly combining multiple notebooks without copying manually

  • Creating a master or final report notebook from modular pieces

  • Developers, data scientists, and students needing consistency across merged files

Not Recommended For:

  • Beginners unfamiliar with the command line

  • When output (plots, tables, results) needs to be preserved (unless re-run)

  • Visually reviewing content before merging


3. Python Script using nbformat

nbformat is a Python library used internally by Jupyter to read, write, and manipulate .ipynb notebook files. You can use it to combine multiple notebooks programmatically, giving you control over:

  • Cell merging

  • Adding metadata or markdown sections

  • Filtering out outputs or unwanted cells

  • Reordering content


Steps:

  1.  Install nbformat

    pip install nbformat

    This gives you access to notebook reading and writing functions.

  2. Organize Your Notebooks

    Place the notebooks you want to merge in the same directory (or note their full paths).

  3. Create the Merge Script

Example:

python

import nbformat


files = ["notebook1.ipynb", "notebook2.ipynb", "notebook3.ipynb"]

merged = nbformat.v4.new_notebook()

merged.cells = []


for fname in files:

    with open(fname, "r", encoding="utf-8") as f:

        nb = nbformat.read(f, as_version=4)

        merged.cells.extend(nb.cells)


with open("merged_notebook.ipynb", "w", encoding="utf-8") as f:

    nbformat.write(merged, f)


what does this Script do

new_notebook()

Initializes a blank notebook

read(..., as_version=4)

Reads a notebook using the nbformat v4 spec

extend(nb.cells)

Adds all cells from the source notebook

new_markdown_cell(...)

Inserts a custom header between notebook sections

nbformat.write(...)

Saves the merged notebook as a .ipynb file

Best for:

  • Developers and data scientists comfortable with Python

  • Automating notebook consolidation in pipelines or projects

  • Customizing the merging process (e.g., add dividers, rename sections)

  • Large-scale or frequent notebook merging

Not Recommended For:

  • Non-programmers or users unfamiliar with scripting

  • Quick, one-time merges of simple notebooks



Pros: Fully automated and customizable 

Cons: Requires Python knowledge



4. Convert to .py, Combine, Then Convert Back




Sample Script:

python

import nbformat


def merge_notebooks(files, output):

    merged = nbformat.v4.new_notebook()

    for file in files:

        with open(file) as f:

            nb = nbformat.read(f, as_version=4)

            merged.cells.extend(nb.cells)

    with open(output, 'w') as f:

        nbformat.write(merged, f)


merge_notebooks(["nb1.ipynb", "nb2.ipynb"], "merged.ipynb")


 Best For:

  • Programmers or developers

  • Customizing the merge process (e.g., filter cells, add metadata)


Pros:

  • Full control over structure, metadata, and order

  • Can be expanded with conditions (e.g., only code cells)

Cons:

  • Requires some programming knowledge

  • Can be overkill for simple tasks



5. Use JupyterLab’s Drag-and-Drop Interface

🧭 Steps:

  1. Open JupyterLab

  2. Open multiple notebooks side by side.

  3. Drag and drop code/markdown/output cells between notebooks.


     Best For:

    • Visual users

    • Medium-sized projects

    • When using JupyterLab IDE


Pros:

  • Easy and visual

  • Preserves outputs and formatting

  • No coding required


    Cons:

  • Only available in JupyterLab (not classic Jupyter)

  • Still semi-manual for larger projects



✅ 6. Combine Using Google Colab


Steps:

  1. Open both notebooks in Google Colab

  2. Use File → Open Notebook or Insert → Code/Text Cell

  3. Optionally use a merging script (e.g., nbformat) inside a code cell.

  4. Copy content as needed or automate merge.

    Best For:

    • Cloud-based workflows

    • Collaboration with others (especially non-technical users)

Pros:

  • No installation required

  • Supports Google Drive integration

  • Good for sharing and collaborative editing

Cons:

  • Requires internet access

  • May have limited file size for very large notebooks


 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page