I have an excel spreadsheet that has 2 used columns. Using Python I have to read the data in those to columns. So far I have a code that lists the item in column A and B on Python GUI. Once that is done I have to write code so that it takes that information and writes into Notepad in a certain way.

So far the code i have that read and print the excel information and actually works is:

1. def excel():
2.	from win32com.client import Dispatch     
3.	xlApp = Dispatch ("Excel.Application")   #Calls for Excel
4.     xlWb = xlApp.Workbooks.Open('IT.xls')    #It finds the workbook
5.      xlSht = xlWb.Worksheets (1)   Goes to sheet 1
6.	dataList = []
7.	for row in range (1,198): #It goes through the 198 items in A and B
8.		for col in (1,2):
9.			dataList.append(xlSht.Cells(row,col))
10.	for item in dataList:
11.		print item

I just have a simple code for notepad but i do not know how to integrate them:

1. def notepad():
2. 	 print
3.	 text_file = open("write.txt","w")
4.	 lines = ["Line 1/n",
5.		  "This is line 2"]
6.	 text_file.writelines(lines)
7.	 text_file.close()

The information needs to appear in notepad in this format without the numbers on the left.

{
define host

        use                     generic-AP

        host_name               Router Name

        alias                   Router Name

        address                IP Address

}

Router name and IP Address changes but everything else remains the same. This needs to get repeated for cells. So in essence this needs to get repeated 198 times.

you could have your def notepad accept a list data type parameter, and the iterate through the list (dataList) and write the information to file. you would basically call the notepad def in the excel def.

you could have your def notepad accept a list data type parameter, and the iterate through the list (dataList) and write the information to file. you would basically call the notepad def in the excel def.

This might sound stupid but is there anyway you could show me an idea. I'm kinda new to Python and I'm struggling.

def notepad(dataList):
    text_file = open("write.txt","w")
    for item in dataList:
        #write the data however you want
        text_file.write('%s\n'%item)
    text_file.close()

def excel():
    from win32com.client import Dispatch     
    xlApp = Dispatch ("Excel.Application")   #Calls for Excel
    xlWb = xlApp.Workbooks.Open('IT.xls')    #It finds the workbook
    xlSht = xlWb.Worksheets (1)   Goes to sheet 1
    dataList = []
    for row in range (1,198): #It goes through the 198 items in A and B
        for col in (1,2):
            dataList.append(xlSht.Cells(row,col))
    notepad(dataList)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.