Splitting large PGN files

Discuss anything you like about chess related matters in this forum.
Post Reply
Paul Macklin
Posts: 20
Joined: Sat Oct 10, 2015 7:36 pm

Splitting large PGN files

Post by Paul Macklin » Sun Nov 15, 2026 3:48 pm

I have a very large opening PGN file, created elsewhere, with numerous opening variations. But file is too large to import into Lichess, for example. To try and split the file into separate variations via Chessbase would take several hours. The file is good, but not that good. Does anyone know of a quick method or program to split this file into its various branches/variations? A search recommended ‘pgn-extract’ but unfortunately that program is not working for me.

Roger de Coverly
Posts: 22729
Joined: Tue Apr 15, 2008 2:51 pm

Re: Splitting large PGN files

Post by Roger de Coverly » Sun Nov 15, 2026 3:52 pm

Paul Macklin wrote: ↑
Sun Nov 15, 2026 3:48 pm
IThe file is good, but not that good. Does anyone know of a quick method or program to split this file into its various branches/variations?
pgn files are nothing special, just text with a different extension. Any text editor or word processor should be able to cope using a cut and paste method to build replacement smaller files. That's provided the file isn't so large that the editor program can't load it.

Ian Thompson
Posts: 4329
Joined: Wed Jul 02, 2008 4:31 pm
Location: Awbridge, Hampshire

Re: Splitting large PGN files

Post by Ian Thompson » Sun Nov 15, 2026 5:27 pm

Paul Macklin wrote: ↑
Sun Nov 15, 2026 3:48 pm
I have a very large opening PGN file, created elsewhere, with numerous opening variations. But file is too large to import into Lichess, for example. To try and split the file into separate variations via Chessbase would take several hours. The file is good, but not that good. Does anyone know of a quick method or program to split this file into its various branches/variations? A search recommended ‘pgn-extract’ but unfortunately that program is not working for me.
Try asking an AI model to do it? Copilot offered this Python script. When I gave it a heavily annotated single game with variations starting at move 9, it returned 5 PGN files, each with one variation in it, and all that line's subvariations. At a brief glance, it all looked OK.

Obviously, the code it's generated is specific to the file I gave it, so better prompting would be needed to create a more general script.

Code: Select all

import io
import os
import zipfile
import chess.pgn

INPUT_FILE = "input.pgn"
OUTPUT_DIR = "move9_variations"
ZIP_FILE = "move9_variations.zip"

os.makedirs(OUTPUT_DIR, exist_ok=True)

with open(INPUT_FILE, "r", encoding="utf-8") as f:
    game = chess.pgn.read_game(f)


def copy_subtree(source_node, target_node):
    """Copy a node and all descendant variations."""
    for variation in source_node.variations:
        new_node = target_node.add_variation(variation.move)

        new_node.comment = variation.comment
        new_node.starting_comment = variation.starting_comment

        try:
            new_node.nags = set(variation.nags)
        except Exception:
            pass

        copy_subtree(variation, new_node)


def path_to_node(node):
    """Return list of moves from root to node."""
    moves = []

    while node.parent:
        moves.append(node.move)
        node = node.parent

    moves.reverse()
    return moves


# Navigate to the position after 9.Nbd2
moves = [
    "e4", "e5",
    "Nf3", "Nc6",
    "Bb5", "a6",
    "Ba4", "Nf6",
    "O-O", "Nxe4",
    "d4", "b5",
    "Bb3", "d5",
    "dxe5", "Be6",
    "Nbd2"
]

node = game

for san in moves:
    move = node.board().parse_san(san)

    for variation in node.variations:
        if variation.move == move:
            node = variation
            break
    else:
        raise ValueError(f"Could not find move: {san}")

created_files = []

# --------------------------------------------------
# File 0: Main line from 9.Nbd2 onwards
# --------------------------------------------------

main_game = chess.pgn.Game()

for k, v in game.headers.items():
    main_game.headers[k] = v

current = main_game

for move in path_to_node(node):
    current = current.add_variation(move)

source = node

while source.variations:
    source = source.variations[0]

    current = current.add_variation(source.move)

    current.comment = source.comment
    current.starting_comment = source.starting_comment

    try:
        current.nags = set(source.nags)
    except Exception:
        pass

filename = os.path.join(
    OUTPUT_DIR,
    "variation_after_9_0_mainline.pgn"
)

with open(filename, "w", encoding="utf-8") as f:
    print(main_game, file=f)

created_files.append(filename)

# --------------------------------------------------
# Files 1..N: Each variation after 9.Nbd2
# --------------------------------------------------

for index, branch in enumerate(node.variations, start=1):

    new_game = chess.pgn.Game()

    for k, v in game.headers.items():
        new_game.headers[k] = v

    current = new_game

    for move in path_to_node(node):
        current = current.add_variation(move)

    branch_node = current.add_variation(branch.move)

    branch_node.comment = branch.comment
    branch_node.starting_comment = branch.starting_comment

    try:
        branch_node.nags = set(branch.nags)
    except Exception:
        pass

    copy_subtree(branch, branch_node)

    filename = os.path.join(
        OUTPUT_DIR,
        f"variation_after_9_{index}.pgn"
    )

    with open(filename, "w", encoding="utf-8") as f:
        print(new_game, file=f)

    created_files.append(filename)

# --------------------------------------------------
# Create ZIP archive
# --------------------------------------------------

with zipfile.ZipFile(
    ZIP_FILE,
    "w",
    zipfile.ZIP_DEFLATED
) as z:
    for filename in created_files:
        z.write(filename, os.path.basename(filename))

print(f"Created {len(created_files)} files")
print(f"Archive written to {ZIP_FILE}")

Paul Macklin
Posts: 20
Joined: Sat Oct 10, 2015 7:36 pm

Re: Splitting large PGN files

Post by Paul Macklin » Sun Nov 15, 2026 7:30 pm

I didn’t think of AI. Before seeing this I ended up editing the main PGN via Chessbase by promoting each of the major variations to ‘main’ variations, and then saving them as separate PGNs after deleting the sub variations. It wasn’t too bad but only because I chose the major variations - splitting them down further would’ve been too much. I will definitely try an AI script for future ones.

Post Reply