Hello!
I`m new here and I hope you could help me. I have to make o program that takes an excel file, counts the number of columns and extracts only two columns from the file and prints them in a .txt file. The number of the columns are inserted from the keyboard.

I need some directions or some tutorials where to get the idea and to learn how to make it.
p.s: I found the xlrd but I want something more simple than that.

Thank you,
Danci Emanuel

Recommended Answers

All 6 Replies

Hi Danci,

There's a number of appropaches you could take to this. Just a few questions..

1. Is it going to be used on 1 Excel file or potentially lots?
2. Does it have to be an Excel file? Could it be an XML or CSV file (created in Excel)?

Chris Fry
Perth, Australia

Hi Danci,

There's a number of appropaches you could take to this. Just a few questions..

1. Is it going to be used on 1 Excel file or potentially lots?
2. Does it have to be an Excel file? Could it be an XML or CSV file (created in Excel)?

Chris Fry
Perth, Australia

The program will use just one Excel file at a time.
The file will be Excel.

The idea that I have in mind is a something like a sieve: the program takes the entire Excel file, counts the columns and extracts only two specific columns.

Thank you,
Danci Emanuel

After rethinking the problem appeared a new option. The file can also be CSV.

Thank you,
Danci Emanuel

In that case you can simply open a csv file and read it like a text file. Additionally there is a csvreader module in python, but I find it's simpler to just open the file as text and do your own splitting/processing.

And to get the user's input you'd use raw_input(prompt)

Oh hey, this might help you, I wrote this helper class a while back while doing some work with excel and I decided to upload this onto github a few days ago.

http://github.com/leegao/pyXLSX/tree/master

Basic Usage:

from xlsx import workbook
def lastnums(t,n): return t[len(t)-n:len(t)]

Workbook = workbook("workbook.xlsx")
extractnums = ("12", "100")
sheet = Workbook.Sheets.Sheet1
for cell in sheet.keys():
	#Cell Name: 'Alpha''Num'
	for num in extractnums:
		if lastnums(cell, len(num)) == num:
			#Do whatever it is that you need to do
			pass

if you read the python csv stuff its pretty good however it will depend on the file format but if the file is not in csv but a simple txt format u can read it as a normal file then read the lines as list and append the selected lines to a new list... and print or write it to a new file this python book python programming on win also has some good stuff, hope it helps u could put a sample formatt up so i can have a look??? also u can chose delimiters and register a dialect to format the way you open a file


hope this helps

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.