Q:

Script to convert an output into an Excel sheet

I have file which contains the following data:
-------------------[ server10 ]-------------------
user1
user2
user99
user100
-------------------[ server20 ]-------------------
user1
user2
user10
user4
-------------------[ server30 ]-------------------
user10
user30
user40

I want to convert this output into Excel sheet, so that I can present ti to my management. Any idea how can do that???  
I want the output into the following format in the Excel sheet:
Hostname            users
server10            user1
                    user2
                    user99
                    user100
server20            user2
                    user10
                    user4 
...
...

Till here "David" helped me with Python (openpyxl). but here i have new column (Last Logon time) which i want to add in excel so here is the example.
    -------------------[ server10 ]-------------------
    user1
    never
    user2
    Oct 17, 2011
    user99
    Jan 01, 2007
    user100
    Feb 02, 2010
    -------------------[ server20 ]-------------------
    user1
    never
    user2
    never
    user10
    jul 17, 2001
    user4
    Dec 25, 1999
    -------------------[ server30 ]-------------------
    user10
    Aug 10, 2012
    user30
    never
    user40
    never

In excel it would be like.
 Hostname            users           Last logon
    server10            user1            never
                        user2            Oct 17, 2011
                        user99           ...
                        user100          ...
    server20            user2
                        user10
                        user4 
    ...
    ...

Notes: username could be different here i gave you example userX but it would be spatel, jim, eric etc...

A:

Another alternative is to use the openpyxl module in python:
from openpyxl import Workbook
import fileinput
import re

wb = Workbook()
ws = wb.get_active_sheet()

servercol = 0
usercol = 1
lastlogoncol = 2
currentrow = 1

# write column headers
ws.cell(row=0, column=servercol).value = 'server'
ws.cell(row=0, column=usercol).value = 'user'
ws.cell(row=0, column=lastlogoncol).value = 'last logon'

# process each line and copy to spreadsheet
for line in fileinput.input("somedata.txt"):
    # user regex to extract server from line
    m = re.match("^[-\[\s]+(?P<server>server\d+)[-\]\s]+$", line)
    n = re.match("^\s+user\d+", line)
    # if server match then write to server column
    if(m):
        c = ws.cell(row = currentrow, column = servercol)
        c.value = m.group('server')
    # elif user match, then write to user column
    elif(n):
        c = ws.cell(row = currentrow, column = usercol)
        c.value = line
    # else write to last logon column
    else:
        c = ws.cell(row = currentrow, column = lastlogoncol)
        c.value = line
        currentrow = currentrow + 1

wb.save('mydata.xlsx')

Note:  this is somewhat fragile, since it depends on the inline data always following a "user" record with a "last logon" record.  If this pattern is not followed, then the process will fail. For example, if you had two consecutive user records, without a "last logon" record in-between.

