Skip to content

Instantly share code, notes, and snippets.

@larshb
Last active March 3, 2019 20:49
Show Gist options
  • Save larshb/db26076724d23c50a02a0fd89ba57070 to your computer and use it in GitHub Desktop.
Save larshb/db26076724d23c50a02a0fd89ba57070 to your computer and use it in GitHub Desktop.
A simple script to parse your ancestry in a compact raw-text format.

GEDCOM ancestry to raw text converter

A simple script to parse your ancestry in a compact raw-text format.

Prerequisite: Python GEDCOM Parser

pip install python-gedcom

Example usage

py gedcom2txt.py -p "birth=1993:name=Lars" -i bolstad.ged
           ┌──Hans Madsen Bolstad (1874 - 1954)
        ┌──Alf Bolstad (1897 - 1958)
        │  └──Anna Marie Olausdatter Grythe (1872 - 1931)
     ┌──Helge Bolstad (1928 - 2009)
     │  │  ┌──Max Richard Böhme (1866 - 1925)
     │  └──Alice Meta Charlotte Voss Voss Böhme (1897 - 1977)
     │     └──Johanna Friedericke Christine Marie Kömm (1865 - 1947)
  ┌──Hans Christian Bolstad (1962)
  │  │     ┌──Kristian Olsen Løkensvingen (1872)
  │  │  ┌──Kristian Julius Kristiansen (1899 - 1975)
  │  │  │  └──Gina Gundersen (1857)
  │  └──Reidun Alvilde Kristiansen (1927 - 2016)
  │     │  ┌──Oluf Nilsen Svenskrusten (1858 - 1919)
  │     └──Minda Oddfrid Myrvang (1906 - 1993)
  │        └──Marthea Edvardsdatter (1875 - 1913)
Lars Henrik Bolstad (1993)
  │        ┌──Lars Larsen Mellemsæter (1836 - 1919)
  │     ┌──Daniel Olaus Larsen Mellemsæther (1879 - 1943)
  │     │  └──Andrea Sofie Abrahamsdatter Martinsdatter Bratvær (1844 - 1925)
  │  ┌──Birger Leonhard Sæther (1924 - 1967)
  │  │  │  ┌──Edvard Jacobsen Øyen (1861 - 1907)
  │  │  └──Charlotte Marie Edvardsdatter Edvardsdatter Øyen (1886 - 1961)
  │  │     └──Karen Andersdatter Rønne (1857 - 1896)
  └──Marit Sæther (1959)
     │     ┌──Nils Kristinus Hansen Elven (1868 - 1943)
     │  ┌──Hans Olbert Elven (1897 - 1989)
     │  │  └──Hanna Margrethe Johansdatter Melkvik (1868 - 1964)
     └──Mary Johanne Elven (1931)
        │  ┌──Johan Pedersen Utseth (1859 - 1928)
        └──Olga Kristoffa Johansdatter Utseth (1904 - 2001)
           └──Otilie Marie Amundsdatter Skreddervik (1866 - 1909)
__author__ = "Lars Henrik Bolstad"
from optparse import OptionParser
from gedcom import Gedcom
class Parents:
def __init__(self, person):
self.father, self.mother = None, None
for parent in gedcom.get_parents(person):
if parent.get_gender() == 'M':
self.father = parent
else: # elif parent.get_gender() == 'F':
self.mother = parent
class Person:
def __init__(self, record):
self.record = record
def __str__(self):
first, last = self.record.get_name()
out = first + " " + last
birth = self.record.get_birth_year()
death = self.record.get_death_year()
if birth > 0:
out += " (" + str(birth)
if death > 0:
out += " - " + str(death)
out += ")"
return out
def isMale(self):
return self.record.get_gender() == 'M'
def print_ancestors(record, generations=1, branch=''):
# Check if person is valid
if not record: return ''
# Limit generations
if not generations: return ''
# Look for parents
parents = Parents(record)
# Print fathers recursively
txt = print_ancestors(parents.father, generations - 1, branch+' ') + '\n'
# Format print
person = Person(record)
line = branch
if branch:
if person.isMale():
line = line[:-1] + '┌──'
branch = branch[:-1] + '│'
else:
line = line[:-1] + '└──'
branch = branch[:-1] + ' '
line += str(person)
txt += line
# Print mothers recursively
txt += print_ancestors(parents.mother, generations - 1, branch+' │')
return txt
if __name__ == "__main__":
# Parse command line arguments
parser = OptionParser()
parser.add_option("-p", "--person",
action="store", type="string", dest="main_person",
help="unique identifier for main person. colon separated string \"surname=[name]:name=[name]:birth=[year]:birth_range=[year-to-year]:death=[year]:death_range[year-to-year]\"")
parser.add_option("-g", "--generations",
action="store", type="int", dest="generations",
help="number of generations to go back", metavar="5",
default=5)
parser.add_option("-i", "--input",
action="store", type="string", dest="input_file",
help="read from input file (.ged)", metavar="FILE")
parser.add_option("-o", "--output",
action="store", type="string", dest="output_file",
help="write to output file (.txt)", metavar="FILE")
#(options, args) = parser.parse_args()
config = vars(parser.parse_args()[0])
# Verify required arguments
if not config["input_file"]:
print("A GEDCOM input-file must be provided\n")
parser.print_help()
exit(1)
# Read input-file
gedcom = Gedcom(config["input_file"], False)
# Look for main person
all_records = gedcom.get_root_child_elements()
main_person, first_person = None, None
for record in all_records:
if record.is_individual():
if not first_person:
first_person = record
if record.criteria_match(config["main_person"]):
main_person = record
if not main_person:
main_person = first_person
print("WARNING: No person matched criteria <%s>"%config["main_person"])
print(" defaulting to %s"%str(Person(main_person)))
# Open output file for writing
outfile = None
if config["output_file"]:
outfile = open(config["output_file"], mode='w', encoding="utf-16")
# Generate tree
txt = print_ancestors(main_person, config["generations"])
# Print or write to file
if outfile:
outfile.write(txt)
outfile.close()
print("Output written to %s"%config["output_file"])
else:
print(txt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment