Export KPvK endings positions (up to black/white symmetry) from ChessBase 15 using python-chess I have the Mega Database 2020 from ChessBase and ChessBase 15. I am using ChessBase 15 to search the Mega Database 2020, my search criterion being all games ended through a KPvK (king and pawn vs. king) up to white/black symmetry.

For this I open the Mega Database in ChessBase 15 and I do "Filter List --> Material". The best I can achieve so far is :



(If I click on "No pieces" as I should, it resets the "Total" four cases, and I don't understand why, so that I am not sure that my filter does exactly want I and gives me all games I really want. I checked on https://en.chessbase.com/support-kb/content/details/1053/Material_searches_with_ChessBase but it did not help very much, but that is another question ...)

This gives a list of games, and if I click on the line corresponding to a game, the position appears for instance as follows :



We see that the board is automatically set up to the position of game defined by the ply where the KPvK (up to colour symmetry) begins.

Is it possible to export all of these positions in a database, retaining the ply move plus players, dates and others usual information?

This could allow to create a KPvK training database. Of course, it is somewhat trivial regarding the nature of the ending, but the principle would be useful for other types of endings.
I used the following python script using python-chess library :

import chess
import chess.pgn

def KPvK_up_to_symmetry_export_to_FEN_func(pgn_file_path, nb_games, output_pgn_file_name):
    output_pgn = open(output_pgn_file_name,"w+")
    games = 0
    pgn_file = open(pgn_file_path, encoding="utf-8-sig")
    while nb_games == 0 or games &lt; nb_games:
        game = chess.pgn.read_game(pgn_file)
        board = game.board()
        has_wanted_position = False
        wanted_fen = ""
        for move in game.mainline_moves():
            board.push(move)
            fen = board.board_fen()

            nb_p = fen.count("p")
            nb_P = fen.count("P")

            nb_n = fen.count("n")
            nb_N = fen.count("N")

            nb_b = fen.count("b")
            nb_B = fen.count("B")

            nb_r = fen.count("r")
            nb_R = fen.count("R")

            nb_q = fen.count("q")
            nb_Q = fen.count("Q")

            nb_k = fen.count("k")
            nb_K = fen.count("K")

            no_pieces = ((nb_n == 0) and (nb_N == 0) and (nb_b == 0) and (nb_B == 0) and (nb_r == 0) and (nb_R == 0) and (nb_q == 0) and (nb_Q == 0))

            cond = ((nb_k == 1) and (nb_K == 1) and (nb_P == 1) and (nb_p == 0)) #KPvK
            cond_symmetrical = ((nb_k == 1) and (nb_K == 1) and (nb_P == 0) and (nb_p == 1)) #KvKP

            #cond = (nb_B == 2 and nb_b &lt; 2)
            #cond_symmetrical = (nb_b == 2 and nb_B &lt; 2)

            if ((cond or cond_symmetrical) and no_pieces):
                has_wanted_position = True
                wanted_fen = board.fen()
                break
            else:
                continue
        #We are out of the loop on game moves, with a FEN or with nothing (if the game hasn't the ending we want)
        if (has_wanted_position):
            for header_key, header_value in game.headers.items():
                output_pgn.write("[" + header_key + " \"" + header_value + "\"]")
                output_pgn.write("\n")
            output_pgn.write("[FEN \"" + wanted_fen + "\"]")
            output_pgn.write("\n")
            output_pgn.write("\n")
        games += 1
    output_pgn.close()
    return None

nb_games_total = 1523849
KPvK_up_to_symmetry_export_to_FEN_func(r"C:\CHESS\PYTHON\megadatabase2020.pgn", nb_games_total, "megadatabase2020_KPvP.pgn")


I did this for the pgn file I got from chessbase by exporting to text PGN my whole megadatabase 2020. Executing this script will produce a pgn file containing positions, that I'll be able to use back in chessbase.