473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating excel masterfile

4
Hi, advanced thanks for any help...

I'm relatively new to programming (two weeks or so), so forgive me if I make some terminological errors here.

I'm using OS X and have a collection of excel files with a lot of data on them. I want to take one particular column out of a particular excel file, and put it into a new excel file. How can I do this?
Jan 7 '10 #1
5 3076
bvdet
2,851 Expert Mod 2GB
Module xlrd is a cross platform tool for reading Excel files. Example:
Expand|Select|Wrap|Line Numbers
  1. import xlrd
  2. wb = xlrd.open_workbook('my_spreadsheet.xls')
  3.  
  4. sheetList = wb.sheet_names()
  5.  
  6. sheet1 = wb.sheet_by_index(6)
  7. sheet2 = wb.sheet_by_name(u'Embeds')
  8.  
  9. for rownum in range(sheet1.nrows):
  10.     print sheet1.row_values(rownum)
  11.  
  12. sixth_column = sheet1.col_values(5)
  13.  
  14. cell_A1 = sheet1.cell(0,0).value
  15. cell_C4 = sheet1.cell(rowx=3,colx=2).value
Read the column of data that you want and create a csv file. If you want the data in an Excel column, you will need to put each data item on a separate line. Examples:
Expand|Select|Wrap|Line Numbers
  1. # add column data on one row
  2. f =open('sixth_column.csv', 'w')
  3. f.write(",".join(sixth_column))
  4. f.close()
  5.  
  6. # add column data in column
  7. f = open('sixth_column1.csv', 'w')
  8. f.write("\n".join(sixth_column))
  9. f.close()
The CSV files can be opened in Excel.

To add another column to an existing CSV file (simplified version):
Expand|Select|Wrap|Line Numbers
  1. fList = [item.strip() for item in open('sixth_column1.csv', 'r').readlines()]
  2. f = open('sixth_seventh_columns.csv', 'w')
  3. f.write('\n'.join([','.join([item1, item2]) for item1, item2 in zip(fList, sheet1.col_values(6))]))
  4. f.close()
To add another column as a row to an existing file:
Expand|Select|Wrap|Line Numbers
  1. f = open('sixth_column.csv', 'a')
  2. f.write("\n"+",".join(sheet1.col_values(6)))
  3. f.close()
Jan 7 '10 #2
Glenton
391 Expert 256MB
You could also do this with VBA. Although I have a nasty feeling that excel for OS X doesn't have VBA. It's been a while since I played with excel, and I don't have it anymore, but it's not very hard to do.

On the other hand it looks like bvdet has shown you a pretty easy way! Nice one bvdet! Very cool
Jan 8 '10 #3
BBMcL
4
This was very helpful, so thank you very much.

After playing around with this for quite a while today, I think I'm well on my way to figuring out the specific task I have.

I can extract a particular column from an excel .xls file just fine. Let's say I extract it from extract_file.xls. I then use the command to join that column with another file - say, 'other_file.csv' - and that seems to work. But when I try to open other_file.csv in Excel, it does not have the column I thought I had added.

Here's what I've been doing specifically:
Expand|Select|Wrap|Line Numbers
  1. import xlrd
  2. Path = '/users/myname/Desktop/'
  3. File = 'extract_file.xls'
  4. wb = xlrd.open_workbook('/users/myname/Desktop/'extract_file.xls')
  5. sheetList = wb.sheet_names()
  6. Sheet1 = wb.sheet_by_index(0)
  7. sixth_column = Sheet1.col_values(5)
  8. for rownum in range(Sheet1.nrows):
  9.               print sixth_column
  10. # With the above steps, I can extract just fine. But I run into trouble when I try to add to the other file - namely, 'other_file.csv'
  11.  
  12. Path = 'users/my_name/Desktop/'
  13. File = other_file.csv'
  14. f = open('other_file.csv', 'w')
  15. f.write("\n".join(sixth_column))
  16. f.close
# But it doesn't change the file...
Jan 9 '10 #4
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code. See posting guidelines.

If you want to add to another file, do not use write ('w') mode. The file will be truncated to 0 bytes. Use append ('a') mode. Pay close attention to my example. Also, you must add parentheses after f.close. f.close returns the function, but you want to call the function.
Jan 9 '10 #5
BBMcL
4
Great. That helps quite a bit. Thank you.
Jan 9 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jeremy Langworthy | last post by:
Hi I am trying to create a MS Excel format CSV but I can't figure out how to get the line feed/carriage return/new record working properly. I am nding each line/record with these characters:...
5
by: Guy Incognito | last post by:
Hello, I've written an asp.net application that creates Excel documents. It works by creating an excel document in XML format. But I wonder if I'm reinventing the wheel. I know that there are...
9
by: Paul | last post by:
Hi all Arggghhh........... The problem.....I want the user to be able to create an excel document and name particular cells in the document where they want the data to be placed and then save...
17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
0
by: Oci-One Kanubi | last post by:
Everything works fine in Access, but when I double-click on the resultant Excel files the first one opens correctly, but subsequent ones, and any other Excel files I try to open, fail to display at...
3
by: Ken Hall | last post by:
Is it possible to create an Excel spreadsheet using VB.NET without opening the Excel application or having to have the Excel application on the operating computer? KH
11
by: Andy Burchill | last post by:
Hi there, I am trying to find out what the best way of creating an excel spreadsheet is, this will need to be done dynamically by an aspx website on the server side. The main way would be to...
0
by: reesa | last post by:
Hi All, I am having problem creating excel pivot table from asp pages. I manage to create excel sheet with teh calculation wanted.But wondering how to make to excel pivot.I know how to create...
2
by: vijayarl | last post by:
Hi Everyone, Am trying to collate the individual xls files into a single xls file.i have written this code. use strict; use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.