I want to output the results of a Python script to the next available column (one without any data in it) of a specified row in an excel spreadsheet (XLS). Below is the portion of my code to do that: -
col_test = 1
-
col_num = open_workbook(subnet)
-
sheet = col_num.sheet_by_index(0)
-
cell = sheet.cell(1,col_test)
-
cell.ctype == XL_CELL_TEXT
-
-
while cell.ctype == True:
-
col_test = col_test + 1
-
cell = sheet.cell(1,col_test)
-
print cell.ctype == XL_CELL_TEXT
-
-
column = cul_test
The results I get are below:
True
True
True
True
True
True
True
True
True
Traceback (most recent call last):
File "/Users/myname/Desktop/TEST_Subnet_Pin g.py", line 48, in <module>
cell = sheet.cell(1,co l_test)
File "/Library/Frameworks/Python.framewor k/Versions/2.6/lib/python2.6/site-packages/xlrd/sheet.py", line 255, in cell
self._cell_type s[rowx][colx],
IndexError: array index out of range
It is correctly starting at column 1, checking to see if theres data there and if so it moves to the next column of that row and checks. When it gets to column 10, a column without any data, I get the IndexError. What I want is the cul_test number so that I can tell my script to output the results in whatever column number cul_test is. How can I set this up so that it doesn't give me any errors when it gets to a column that has absolutely no data in it at all? My expectation was that the WHILE would quit after finding a column with no data and I would have that cul_test number. I am not a programmer so this is all pretty new to me. It's probably something simple but I can not figure out what. Any help would be greatly appreciated and if you need more info please let me know. Thanks!
5 9704 Glenton 391
Recognized Expert Contributor
Hi
I haven't really used python with excel before, but can tell you a basic problem with your code (By the way, you'd probably have got answers sooner if you used code tags, because it's much easier for people to read!)
Thought 1: I could be way off here, since I'm not familiar with the functions you're using, but is it possible that cell.ctype is always true, and so you're moving all way across your spreadsheet until you run out of columns?
Try run it again, but change the print line to:
print col_test, cell.ctype
and post back.
Thought 2: If it's "working" in the sense that it's finding the first blank column and returning an error at cell = sheet.cell(1,co l_test), then there's probably another way to do it.
bvdet 2,851
Recognized Expert Moderator Specialist
I have played with xlrd a few times. It's great for reading an Excel spreadsheet, but you cannot write to an Excel spreadsheet. You could write all the data to a CSV file, then import the file back into Excel, or you could use win32com.client .Dispatch("Exce l.Application") . Example using win32com: - import win32com.client
-
xlApp = win32com.client.Dispatch("Excel.Application")
-
xlApp.Visible=0
-
xlWb = xlApp.Workbooks.Open(r"D:\SDS2_7.0\macro\win32com\Read.xls")
-
print xlApp.Worksheets("Sheet1").Name
-
xlApp.Worksheets("Sheet2").Range("B1").Value = "green"
-
sheet = xlApp.Worksheets("Sheet3")
-
sheet.Range("C3").Value = "money"
-
sheet.Range("D4").Value = 9999
-
print sheet.Range("C3").Value
-
print sheet.Range("D4").Value
-
xlWb.Close(SaveChanges=1)
-
xlApp.Quit()
Use xlrd.sheet.Shee t object method rows() and row_values to read the data. Example: - >>> import xlrd
-
>>> wb = xlrd.open_workbook('workbook.xls')
-
>>> sheet1 = wb.sheet_by_index(6)
-
>>> sheet1.nrows
-
68
-
>>> sheet1.row_values(13)
-
[u'GARAGE L1', '', u'POUR 1', u'L1', 40196.0, u'Embeds - Vault', u'Detail D03 ?', 21.0, 40175.0, '', u'None Shown', '', '', '', '']
-
>>> len(sheet1.row_values(13))
-
15
-
>>> sheet1
-
<xlrd.sheet.Sheet object at 0x00CA7610>
-
>>>
Thanks Glenton. Code tags, I will remember that.
cell.ctype will return False when it checks a cell that has no data in it PROVIDING there's data somewhere within the same column, just on a different row, than the cell resides. i hope that makes sense. where I get the error is when it hits a column that has absolutely no data in any of it's rows.
I agree with your second thought. I'm certain there's another way to do it.
bvdet 2,851
Recognized Expert Moderator Specialist
If you would use sheet_object.ro w_values(row_nu mber) you would know exactly how may columns are occupied and avoid the error. See my example code above. You could also directly read the number of columns like this: - wb = xlrd.open_workbook('workbook.xls')
-
sheet1 = wb.sheet_by_index(1)
-
numCols = sheet1.ncols
Then you know which column is unoccupied.
Yes thanks bvdet. Took me a little while to test it and respond from your previous post but sheet_object.ro w_values(row_nu mber) worked perfectly. That's exactly what I needed. Thanks for your help
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: jquest |
last post by:
Hi Again;
I have had help from this group before and want to thank everyone,
especially PCDatasheet.
My database includes a field called HomePhone, it uses the (xxx)xxx-xxx
format to include area code.
When a customer calls, I currently use Ctrl F with the HomePhone field
highlighted. Then I enter the last 4 digits and use the find next option.
This is cumbersome, so I have tried several methods (including a macro)
using comand...
|
by: Roger Twomey |
last post by:
I have a form that allows a user to upload a microsoft Excel Spreadsheet. (I
am testing with one created in Excel 2000)
The spreadsheet is uploaded correctly.
It is then supposed to be read and display in a datagrid. When the code
executes I get:
System.Data.OleDb.OleDbException: Could not find installable ISAM
|
by: garry.oxnard |
last post by:
Can anyone help me to solve a problem which involves switching from
Access to Excel (then back to Access) programatically please?
I have an Excel template which, on open, also opens an Access database
- containing a list of addresses.
Sequence of events is =
(1) Excel template opens in its default XXX.xls filename.
(2) Code runs to save the spreadsheet as XXX.xls.
(3) User clicks a button to open an Access database containing an
|
by: Hokiecow |
last post by:
I'm trying to import specific columns from an excel file
(Requirements.xls) into an access table (tblRequirements). Using VBA,
I'm able to import the entire excel file into table
(tblImportRequirements). Instead of going row by row of
(tblImportRequirements) and copying the fields needs to
(tblRequirements) it would be faster to just copy the column and over
write the other table's column. I'm not sure how to copy a few
columns from...
|
by: Problematic coder |
last post by:
Hi,
I have just been asked to create an app that will import a specific
column from an excel spreadsheet, something like sheet1 columnD or a
column with the first row with a value of 'ID' whichever would be
easier. The rest of what I need to do once I have the data I am ok
with but the connecting to the spreadsheet and extracting a column
into an array or dataset or whatever would work best I have no clue.
I would appreciate some...
| |
by: Problematic coder |
last post by:
Hi,
I have just been asked to create an app that will import a specific
column from an excel spreadsheet, something like sheet1 columnD or a
column with the first row with a value of 'ID' whichever would be
easier. The rest of what I need to do once I have the data I am ok
with but the connecting to the spreadsheet and extracting a column
into an array or dataset or whatever would work best I have no clue.
I would appreciate some...
|
by: lildiapaz |
last post by:
Can someone please explain to me how to select a specific column in an excel spreadsheet and import the information in that column to an sql table in a c# windows application. Let's say my excel spreadsheet is full of first and last names and dates. What I want to do is take column "A" and insert the names in that column into an sql table with the column name "first_name". And I want to do the same thing for the last name column
Example...
|
by: JohnMcA |
last post by:
Hi,
I'm having problems accessing values in an Excel spreadsheet column which contain a ; delimiter. If there is only one value in the column, then it is correctly returned without any problem.
However, any other columns which also contain the ; delimiter work without problems, so I'm thinking that this has something to do with the spreadsheet column. For that reason, I setup a new column in the spreadsheet and copied & pasted some values...
|
by: ShadowLocke |
last post by:
Hi,
Im not very experienced with XSL, this is my first run in with it. I found an XSL file that converts a dataset to an excel readable format.
The problem I have is when the dataset has a null value for a column, it does not add an empty element for the column. Is there any way I can detect this?
Excel.xsl:
<xsl:stylesheet version="1.0"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
|
by: sumithar |
last post by:
I am trying to create a spreadsheet from mysql data. I have the
requisite headers in place for content type and all that.
All I am doing is executing the SQL query and displaying the data
formatted in an HTML table.
Whatever width I assign to the HTML table columns ( using <td
width="nnn"it doesn't have any impact on the width of the
spreadsheet column- that is always staying the same.
Any idea how to control it?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |