from pathlib import Path import argparse,zipfile,xml.etree.ElementTree as E,hashlib,json,re parser=argparse.ArgumentParser();parser.add_argument('source',type=Path);parser.add_argument('--out',type=Path,required=True);args=parser.parse_args() ns={'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'} def clean(s):return re.sub(r'\s+([,;)])',r'\1',re.sub(r'([(])\s+',r'\1',re.sub(r'\s+',' ',s))).strip() def text(el):return clean(' '.join(x.text or '' for x in el.findall('.//w:t',ns))) z=zipfile.ZipFile(args.source);root=E.fromstring(z.read('word/document.xml'));body=root.find('w:body',ns);tables=[];sections={};heading='';paragraphs=[] for node in body: if node.tag.endswith('}p'): value=text(node);paragraphs.append(value) if re.match(r'^[1-8]\s*\.0 ',value):heading=re.match(r'^([1-8])\s*\.0 ',value).group(1)+'.0';sections[heading]=[] elif value:sections.setdefault(heading,[]).append(value) elif node.tag.endswith('}tbl'):tables.append([[text(cell) for cell in row.findall('w:tc',ns)] for row in node.findall('w:tr',ns)]) assert len(tables)==13 and len(tables[0])==12 and len(tables[2])==11 provinces=['Cabo Delgado','Gaza','Inhambane','Manica','Maputo','Nampula','Niassa','Sofala','Tete','Zambézia'] def locations(value):return [p for p in provinces if re.search(r'(?