wegen/sosci2word/sosci2word.py

56 lines
1.5 KiB
Python

#!/usr/bin/python3
#
# sosci2word - Einlesen eines SoSci-Fragebogens und umwandeln in ein Word-Dokument
#
# (c) 2023 Hermann Voßeler
# This code is free software and can be used under the terms of the MIT License.
#
from xml.etree import ElementTree
#-----------------------SETUP
SOSCI_XML='ThesisFragebogen.2023-04-19.xml'
WORD_TEMPLATE='Thesis-Fragebogen.docx'
WORD_OUTPUT='Thesis-Fragebogen-import.docx'
#-----------------------SETUP
def convertSosci2Word():
#
root = ElementTree.parse(SOSCI_XML).getroot()
for section in root.findall('section'):
title = section.find('title')
id_prefix = getSpecificID(section)
print("Section: Title »%s« Prefix=%s" % (title.text,id_prefix))
for question in section.findall('question'):
title = question.find('title').text
desc = question.find('description').text
qustionType = question.find('class').text
id_suffix = getSpecificID(question)
id_suffix = int(id_suffix)
questionID = "%s%02d" % (id_prefix, id_suffix)
print("Question: ID=%s desc=%s type=%s title=%s " % (questionID, desc, qustionType, title))
# Hilsmittel für SoSci-XML: extrahiert ein ID-Element, das eingeschachtelt in <attributes.specific> liegt
def getSpecificID(element):
attribspecific = element.find('attributes.specific')
return next(attr.text for attr in attribspecific.findall('attr') if attr.get('id') == 'id')
if __name__=='__main__':
convertSosci2Word()