a helper for publishing chess games

pgn2html.py 1.2 - generate html-code from a pgn-file

new version 1.2 with important bugfix - 02.06.2013

This small tool is designed to generate a HTML-code block from the notation block of a single game chess file (PGN-file).

- the pgn-code looks like this for example:

1. e4 c5 2. d4 cxd4 3. c3 e5 4. cxd4 exd4 5. Qxd4 Nc6
6. Qd1 Nf6 7. Nc3 Bb4 8. Bd3 0-0 {a small opening advantage: gaining the pawn on e4}
9. Nf3 Re8 10. 0-0 Bxc3 11. bxc3 Nxe4 12. Bxe4 Rxe4 13. Ba3 b6 14. Re1 d5
15. Nd4 Bb7

- the generated html source code:

<span id="not">1.&nbsp;e4&nbsp;c5 2.&nbsp;d4&nbsp;cxd4
3.&nbsp;c3&nbsp;e5 4.&nbsp;cxd4&nbsp;exd4 5.&nbsp;Qxd4&nbsp;Nc6
6.&nbsp;Qd1&nbsp;Nf6 7.&nbsp;Nc3&nbsp;Bb4 8.&nbsp;Bd3&nbsp;0-0</span>
<span id="com">a small opening advantage: gaining the pawn on e4</span>
<span id="not">9.&nbsp;Nf3&nbsp;Re8 10.&nbsp;0-0&nbsp;Bxc3
11.&nbsp;bxc3&nbsp;Nxe4 12.&nbsp;Bxe4&nbsp;Rxe4 13.&nbsp;Ba3&nbsp;b6
14.&nbsp;Re1&nbsp;d5 15.&nbsp;Nd4&nbsp;Bb7 15.&nbsp;Nd4&nbsp;</span>

- the web look:

1. e4 c5 2. d4 cxd4 3. c3 e5 4. cxd4 exd4 5. Qxd4 Nc6 6. Qd1 Nf6 7. Nc3 Bb4 8. Bd3 0-0 a small opening advantage: gaining the pawn on e4 9. Nf3 Re8 10. 0-0 Bxc3 11. bxc3 Nxe4 12. Bxe4 Rxe4 13. Ba3 b6 14. Re1 d5 15. Nd4 Bb7 15. Nd4 

The span-tags for notation and commentary can be customized in the header of the python-script.
The main task of the tool is to replace whitespace with &nbsp; - not everywhere, but inside the notation of a single move:

1.&nbsp;e4&nbsp;c5 instead of 1. e4 c5

...so that the whole move is treated like a word, preventing ugly line-breaks in a block layout like this:

1. e4 c5 2. d4 cxd4 3. c3 e5 4. cxd4 exd4 5. Qxd4 Nc6 6. Qd1 Nf6 7.
Nc3 O-O 8. Bc1-d2 d7-d6 9. h2-h3 a7-a6 10. Qd1-e2 e6-e5 11. Nc3-d5
Nf6xd5 12. e4xd5 Bb4xd2+ 13. Qe2xd2 Nc6-e7 14. c2-c4 Ne7-g6 15.
Bd3xg6 f7xg6 16. b2-b4 Bc8-d7 ...


to function properly, the pgn-notation-text should regard the following rules:

  • comments in (curly brackets '{}') need a whitespace in front of the first bracket

  • moves must be separated like this: 1. e4 ... {blabla} 1. ... c5
    and never this way: 1. e4 {blabla} 1. ... c5

that's all - enjoy!


open a Shell-Window in the directory where 'pgn2html.pyc' is located (Strg-D if you use Freecommander)
and type in:

python pgn2html.pyc d:\Chess\2012.04.30_genialchess.de.pgn

...that should generate a text-file named: 'd:\Chess\2012.04.30_genialchess.de.html'
just copy its content to your html-source-code file then.


download pgn2html.pyc

09.05.2012 / last updated: 02.06.2013