# Filename: nbo_charge_script.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 = 'Summary of Natural Population Analysis' endstring = '* Total *' count = 0 startlinenumber = 0 endlinenumber = 0 for line in userfile: if startstring in line: startlinenumber = count + 6 if endstring in line: endlinenumber = count - 2 count = count + 1 # -- write to the excel file headers = ['Atom Type', 'Atom No.', 'Natural Charge'] worksheet.write_row('A1', headers) with open(userinput) as fp: for i, line in enumerate(fp): if i >= startlinenumber and i <= endlinenumber: linecontent=[] elementcount = 0 for u in line.split(): if elementcount < 3: linecontent = linecontent + [u] elementcount = elementcount + 1 rownumber = i - startlinenumber + 2 rownumberstring = 'A'+str(rownumber) worksheet.write_row(rownumberstring, linecontent) print("Generated " + workbooktitle + " file") print("")