Saving data of TrialHandler object in a .xlsx file

Good afternoon,
I am currently trying to set up an experiment with multiple choice questions, and I am having a hard time in saving the answers correctly.
The stimuli used are named squares on the left of the possible answers, which are employed as tick boxes.
I would like to save the name of the clicked square in a .xlsx file as in the suggested demos.
I am also using a loop for the different trials, and so the builder automatically creates a TrialHandler object named “trials”. However, unlike in the example of the “stroop” experiment such object is not saved: I went to look at the .py code associated with the .psyexp file.
Hence, I manually added the chunk of code missing in my .psyexp.

get names of stimulus parameters

if trials.trialList in ([], [None], None):
    params = []
else:
    params = trials.trialList[0].keys()
# save data for this loop
trials.saveAsExcel(filename + '.xlsx', sheetName='trials',
    stimOut=params,
    dataOut=['n','all_mean','all_std', 'all_raw'])

However, with this adjustment the windows of the experiment closes correctly but the program freezes and needs to be manually stopped.
A .xlsx file is created, but I fear that the anomalous termination of the program may results in data missing or not being saved correctly.
In addition, the changes I made to the .py code do not seem to be saved.

Therefore, first I wonder whether the saving process of the TrialHandler object can be programmed directly from the high-level interface and whether this could solve the freezing of the program.
Also, is there a way to modify the python program in such a way that the changes are enforced in the .psyexp file?

Thank you in advance for you attention