How to fetch the cell value of excel sheet

Asked by Muthumanikandan

I am new to sikuli python script:

Now i am facing a problem in selecting a cell value from excel sheet and want to put the same value in my windows application.

I dont know how to fetch the cell value from excel to my application,Kindly give me a solution.

If possible please explain the code with simple example.

I already go through the faq2208 i am not clear with it, so give me some other solutions.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Muthumanikandan
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1
Revision history for this message
Muthumanikandan (nmuthu1) said :
#2

We are not clear with faq2208, so can you please explain with simple example.

i have few questions on that faq,
1)Is i need to convert xlsx to csv,if yes help me to convert

please provide fully coded explanation

Revision history for this message
Manfred Hampl (m-hampl) said :
#3

See the last paragraph of faq 2208

*** looking for docs
 xlrd1 is based on the original xlrd - download from here
http://pypi.python.org/packages/source/x/xlrd/xlrd-0.7.1.zip

This one contains some useful docs and examples in the contained xlrd folder and you might use this one as well.

(Remark: the current version of xlrd is 1.0.0)

**Quick start**:

```
    import xlrd
    book = xlrd.open_workbook("myfile.xls")
    print "The number of worksheets is", book.nsheets
    print "Worksheet name(s):", book.sheet_names()
    sh = book.sheet_by_index(0)
    print sh.name, sh.nrows, sh.ncols
    print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
    for rx in range(sh.nrows):
        print sh.row(rx)
    # Refer to docs for more details.
    # Feedback on API is welcomed.

```

Revision history for this message
RaiMan (raimund-hocke) said :
#4

please consult the docs of the xlrd module as mentioned in the faq

xlrd allows, to directly access Excel sheets per cell and more.

converting the Excel sheet to csv is a fallback option and should always work.

The docs for every Python module have code examples and the net is full of it.

Revision history for this message
masuo (masuo-ohara) said :
#5

If you are using Sikuli IDE, try following codes.

import xlrd
import datetime
EXCELFILE = "C:\\temp\\book1.xls"

book = xlrd.open_workbook(EXCELFILE)
sheet = book.sheet_by_index(0)
for col in range(sheet.ncols):
    for row in range(sheet.nrows):
        celltype = sheet.cell_type(row,col)
        value = sheet.cell_value(row,col)
        if celltype == 1:
            print "col:%d row:%d value:%s" % (col,row,value)
        elif celltype == 2:
            print "col:%d row:%d value:%d" % (col,row,value)
        elif celltype == 3:
            s = datetime.datetime(*xlrd.xldate_as_tuple(value,book.datemode)).strftime("%Y/%m/%d %H:%M:%S")
            print "col:%d row:%d value:%s" % (col,row,s)

Revision history for this message
Muthumanikandan (nmuthu1) said :
#6

Thanks masuo, that solved my question.

Revision history for this message
Muthumanikandan (nmuthu1) said :
#7

Thanks masuo,

The above code imported all my excel sheet values,but i want to fetch a single column (For ex: cell: H41) and i want to paste that values in m application text field

Revision history for this message
masuo (masuo-ohara) said :
#8

When strings is in cell H41

row = 40
col = 7
value = sheet.cell_value(row,col)

click("textbox.png") #click your application text field
paste(value) #paste strings from CELL

Revision history for this message
Muthumanikandan (nmuthu1) said :
#9

masuo thanks alot,

This solved my problem..

Revision history for this message
Bharathi A (rathisoft) said :
#10

Friends here is my case -

Step 1: Fetch the value of a CELL from one Excel sheet
status: success
Step 2: Paste that value in another cell (say C30) in another Excel sheet
status: Not success.

Need help for acheiving Step 2.

TiA !

Revision history for this message
Manfred Hampl (m-hampl) said :
#11

@Bharati A:

1. Please do not piggyback on somebody else's question, but create a new question document for your problem. There is no way that the status of your problem can be tracked, because the original question document is already marked as "solved".

2. xlrd (Excel Read) provides functionality to READ information form excel files and does not allow changing them.
You have to use other software. Maybe xlwt (Excel Write) can help with your problem.

Revision history for this message
Bharathi A (rathisoft) said :
#12

Thanks Manfred for those pointers !