# Filename: chelpg.py # -- module import import sys import xlsxwriter # -- open / edit file userinput = input("Enter filename: ") userfile = open(userinput, 'r') # -- create a new excel file and add a worksheet titlename = '' if userinput.endswith('.log'): titlename = userinput[:-4] workbooktitle = titlename + '.xlsx' workbook = xlsxwriter.Workbook(workbooktitle) worksheet = workbook.add_worksheet() # -- find relevent section from the chelpg calculation log file startstring = 'Fitting point charges to electrostatic potential' endstring = 'Sum of ESP charges' count = 0 startlinenumber = 0 endlinenumber = 0 for line in userfile: if startstring in line: startlinenumber = count + 4 if endstring in line: endlinenumber = count - 1 count = count + 1 # -- write to the excel file headers = ['Atom No.', 'Atom Type', 'Charge'] worksheet.write_row('A1', headers) with open(userinput) as fp: data = [] for i, line in enumerate(fp): if i >= startlinenumber and i <= endlinenumber: linecontent=[] for u in line.split(): linecontent = linecontent + [u] rownumber = i - startlinenumber + 2 rownumberstring = 'A'+str(rownumber) worksheet.write_row(rownumberstring, linecontent) print("Generated " + workbooktitle + " file") print("")