Want to use win32com to access excel sheets - try python package xlrd

Asked by Jessica Tan

i run the following code in sikuli:

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')

ImportError: No module named win32com.

So how do i import python standard library module to sikuli? Just for you information, pywin32-214.win32-py2.6.exe is installed on my machine.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Python modules can only be imported if fully written in Python language or ported to the Jython/Java environment (which is the Python interpreter used in Sikuli).

I guess win32com is a C-type based Python module. Those cannot be used together with Sikuli/Jython.

To dispatch apps on windows you have to use the Sikuli class App.

If you want to stay with the Python interpreter (where you can use win32com) and want to integrate Sikuli, you have to produce self contained Sikuli scripts, export them as .skl and run them from commandline using e.g. os.popen().

You may implement a sikuli script dispatcher subprocess (running as Jython) or use XML-RPC (look https://answers.launchpad.net/sikuli/+faq/1331)

Revision history for this message
Jessica Tan (jessicatan12) said :
#2

Thanks for your quick response.
Reason for importing win32com in sikuli is because when i use sikuli to open an excel file, i have problem in selecting the specific cell. That's why i am thinking of importing win32com, so that i can have better control on cell selection.

I wrote the following code in sikuli to select Cell C1 in excel spreadsheet.

location = find (Image of Cell "C" )
click(location.getCenter().below(15)) # move down by 15 to select C1

But the above code failed to select C1, it selects Cell A instead. Could you please advise how should i modify the code in order to select specific cell?

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

Try the package xlrd, that is fully written in Python language and works like a charm together with Sikuli. It can read and write excel sheets.

I have tested it and know how to get it running (if you need more help then).
You have to install it (easy_install) in normal python and then copy the xlrd package to a place, where you can import it easily (or just use it from where it is)

download: http://pypi.python.org/pypi/xlrd

I'm planning to write a howto in the FAQ's, so your experiences are welcome.

Revision history for this message
Allan (allan-yap) said :
#4

Hi,

I have to admit, i am newbie in Sikuli and wanted to use Sikuli with xlrd package. After read through this thred and go through the website try to find the relevant notes, i still failed to link the xlrd into Sikuli. I have got the Sikuli X-1.0rc2, Python 2.7, Jython 2.5.2 installed in my 32bit windows XP using the standard exe installation. I have even download the xlrd 0.7.1 and perform the python setup.py build & install.

And when i open up the sikuli, type "import xlrd" i am hitting "ImportError: No module named xlrd"... I know i must have still missing something in between in order to get this working.....

So, how do i get this working this time?

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

@Allan

-- Python 2.7
does not matter for Sikuli - it is not used. Sikuli uses Jython

-- Jython 2.5.2
Sikuli comes bundled with Jython 2.5.1, which is internally used if you run your script according to the defined approaches (in IDE or from command line).
If you want to run your scripts with Jython 2.5.2, you may do this from command line:
see: https://answers.launchpad.net/sikuli/+question/141302
only read and use the summary (reading the comments does not help more and might be confusing only)

... but normally there is no need to use Jython 2.5.2

-- xlrd
there is no need to run the install (this is for Python only).

just download the zip version and unzip to a directory of your choice.
in your script add:
dir_xlrd = "absolute path to the directory xlrd-0.7.1"
if not dir_xlrd in sys.path: sys.path.append(dir_xlrd)
import xlrd

now you can use it according to the xlrd docs.

Revision history for this message
Allan (allan-yap) said :
#6

Hi RaiMan,

You are Great!!! With your steps, i finally managed to get into xlrd from sikuli.

Cheers :)

Revision history for this message
Elyashevskyy Ostap (osai) said :
#7

what about accessing Outlook2010 via com ? are there any solutions ?

thanks,
Ostap

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

Using Sikuli Python/Jython or Java?

What does Google tell you?

Where else is it possible to "accessing Outlook2010 via com" by which approach?

Talk about your existing solution, that you want to use with Sikuli.

Revision history for this message
Elyashevskyy Ostap (osai) said :
#9

I need to clear up any folder from any email account of Outlook2010 from Sikuli

I tried to do it via Sikuli but it does not work with win32com.

Now I am using the next solution via Python app and then launching it via Sikuli / Jython as externall app:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

fldr = outlook.GetDefaultFolder(6) // Constant is defined for each default folder

messages = fldr.Items
print "number of messages in inbox:", messages.Count
for msg in messages:
       msg.Delete()

Do you know another solutions ? Also I am interesting how to clean up not only default folders (Inbox, ....)

Thanks,
Ostap

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

--1.
I do not have any experience with this kind of Outlook scripting.

--2. win32com
If you want to use this approach, you have to stick with Python.
This is not available for the Sikuli/Jython/Java environment since it is C-based.

--3. Sikuli + Python
Depending on the reason, why you want to use Sikuli for that or together with that and wether the focus lies on the Sikuli features or on the Python/win32com stuff, you have to decide, what is the leading system.
There are some solutions possible how to integrate Sikuli and Python. If you want some more tightly coupled solution than using each other as external apps, you have to implement your stuff as functions that can be dispatched using some control routine.
You might use sub processing, pipes, XML-RPC (this might be the easiest one to implement) and some more.

It is some challenge ;-)

Revision history for this message
Elyashevskyy Ostap (osai) said :
#11

3. The leading system is Sikuli / Jython, because test framework is already implemented on it.

For now I am cleaning email folders via Sikuli (using image recognition)
But it takes a lot of time and is not always success.

Now I see the next solutions:

1. (The best at the moment) Use Redemption library: http://www.dimastr.com/redemption/RDOAccount.htm
It provides COM interfaces to work with email accounts, messages, etc, ...
2. Or just use ole32com & Outlook com interfaces (this solution is not very good, since there are some limitations and API is slightly different for different versions of OL)

Thanks for advises,
Ostap

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

--- using .dll's
What ever sort of DLL you want to use with Sikuli, you will not have direct access to any API. You would have to make some JNI-wrapper or use something like SWIG.

In this area, I can not help you further, since this has nothing to do with Sikuli. It is the general challenge for Java applications to access native dynamic libraries.

--- Why do you not use some simple VBA macros, that could be easily integrated with Sikuli's GUI handling?

--- For now I am cleaning email folders via Sikuli (using image recognition)
But it takes a lot of time and is not always success.

- this is my experience: I am sure your scripts could be made much faster and more reliable, by restricting search regions, use calculated click points and keyboard shortcuts where possible.

Revision history for this message
Elyashevskyy Ostap (osai) said :
#13

 Why do you not use some simple VBA macros, that could be easily integrated with Sikuli's GUI
>> I am not familiar with VBA macros (((

this is my experience: I am sure your scripts could be made much faster and more reliable
>> Tried to do it but need to support OL2003/2007/2010, so every client needs separate images

I have completely solved the problem, so the solution I use:
1. To clean up IMAP folders I use IMAP lib for Python (clean directly from IMAP server, very fast and what I need): http://stackoverflow.com/questions/3180891/imap-deleting-messages
2. To clean up Exchange folders: using win32com
3. Invoking the script from Jython via "openApp"
python script.py 'folder_to_clean' 'email_account'

It works pretty good and allows to use the same code for any email client (in case of IMAP),
I know that it is better to communicate via XML-RPC as you suggested, but at the moment approach above is what I really need.

Thanks again.

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

@ Elyashevskyy
Thanks for the detailed feedback. I will make a faq for this "How to access MS Office apps" stuff with your solution.

--- I know that it is better to communicate via XML-RPC
That is not absolutely true. With XML-RPC you can setup a server, that waits for requests to process them. This only makes sense, if you want to repeat some tasks relatively often, so that startup and general setup cost too much time.
I think in your case, this does not matter.

Can you help with this problem?

Provide an answer of your own, or ask Jessica Tan for more information if necessary.

To post a message you must log in.