Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save plopp/98f4cce51000525a755af7f8840f52e1 to your computer and use it in GitHub Desktop.
Save plopp/98f4cce51000525a755af7f8840f52e1 to your computer and use it in GitHub Desktop.
import xlsxwriter
import sys
#### Kvicksundspokalen parser
#Parse file
#Extract fields
#Sort by date?
#Format xml output
#Add to sheet
#Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
reload(sys)
sys.setdefaultencoding('utf-8')
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
worksheet.set_column('C:C', 15)
worksheet.set_column('E:E', 20)
worksheet.set_column('F:F', 15)
worksheet.set_column('G:G', 30)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some simple text.
worksheet.write('A1', "Lagnamn", bold)
worksheet.write('B1', "Klass", bold)
worksheet.write('C1', "Anmdatum", bold)
worksheet.write('D1', "Status", bold)
worksheet.write('E1', "Namn", bold)
worksheet.write('F1', "Telefon", bold)
worksheet.write('G1', "Mail", bold)
directory = "DIRECTORY"
rowCount = 1
for filename in os.listdir(directory):
if filename.endswith(".txt"):
# print(os.path.join(directory, filename))
with open(filename, "r") as infile, open("mail_adresser.txt", "w") as outfile:
for line in infile:
if "Lagnamn : " in line:
worksheet.write(rowCount, 0,line.split(" : ")[1])
elif "Klass : " in line:
worksheet.write(rowCount, 1,line.split(" : ")[1])
elif "datum : " in line:
worksheet.write(rowCount, 2,line.split(" : ")[1])
elif "Status : " in line:
worksheet.write(rowCount, 3,line.split(" : ")[1])
elif "Namn : " in line:
worksheet.write(rowCount, 4,line.split(" : ")[1])
elif "Telefon : " in line:
worksheet.write(rowCount, 5,line.split(" : ")[1])
elif "E-mail : " in line:
outfile.write((line.split(" : ")[1])+",")
worksheet.write(rowCount, 6,line.split(" : ")[1])
rowCount = rowCount + 1
workbook.close()
continue
else:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment