-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgedcom2gexf.py
executable file
·48 lines (44 loc) · 1.59 KB
/
gedcom2gexf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Works with Python2 and Python3
#
# It requires:
#
# - networkx package for graph handling
# - gedcompy package to parse gedcom file
#
# python -m pip install networkx
# python -m pip install gedcompy
#
# Be careful, this package may collide with python-gedcom.
import gedcom
import networkx as nx
import os
def gedcom2gephi(gedcomFilename='gedcom.ged', gephiFilename=None):
getName = lambda n: n.name[0]+' '+n.name[1]
getId = lambda n: n.id[1:-1]
getFamilyName = lambda n: n.name[1]
g = gedcom.parse(gedcomFilename)
dg = nx.DiGraph()
for p in g.individuals:
if p.id not in dg:
dg.add_node(getId(p), label=getName(p), name=getName(p), familyName=getFamilyName(p))
for p in g.individuals:
if p.father:
dg.add_edge(getId(p.father), getId(p))
if p.mother:
dg.add_edge(getId(p.mother), getId(p))
if gephiFilename is None:
gephiFilename = os.path.splitext(gedcomFilename)[0] + '.gexf'
nx.write_gexf(dg, gephiFilename)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description = 'This script converts a gedcom file to a gexf file')
pa = parser.add_argument
pa('-g','--gedcom', type = str, default = 'my_gedcom_file.ged',
help = 'Gedcom filename')
pa('-o','--outputGexf', type = str, default = None,
help='Optional output name. If not provided, a filename will be generated from the gedcom filename')
args = parser.parse_args()
gedcom2gephi(gedcomFilename=args.gedcom, gephiFilename=args.outputGexf)