Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [002] Ian Ozsvald
Member: 107 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_: .. image:: http://procasts.co.uk/media/procasts_sma ...

Talking to Excel with pyWin32 [ID:619] (2/6)

in series: COM and Python with pyWin32

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

Here we test pyWin32 (build 210) on Windows XP using Python 2.5 with Excel 2000.

Topics:
  • Starting Excel using COM, making it Visible
  • Referencing a workbook and sheet
  • Reading/writing data to Excel cells
  • Setting a sheet's name
  • 0 or 1 based indexing
For reference and further notes on 0 and 1 based indexing:

You'll want to buy Python Programming on Win32, it is a great (and the only!) guide for pyWin32.

# Notes for interacting with pyWin32 (http://sourceforge.net/projects/pywin32/)
# Running this script won't do much, instead just look at it for reference

# import the Dispatch library, get a reference to an Excel instance
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")

# make Excel visible (1 is True, 0 is False)
xlApp.Visible=1

# make a new Workbook
xlBook = xlApp.Workbooks.Add()
# get a reference to the same workbook - these two do the same thing
xlBook = xlApp.Workbooks(1) 

# get a reference to the first sheet
xlSheet = xlBook.Sheets(1)
# reference a cell and set a value
xlSheet.Cells(1,1).Value="hello"

# extract the value from a cell
a_value = xlSheet.Cells(1,1).Value

# empty cells have value of None
xlSheet.Cells(1,2).Value == None

# use formula as we would in excel
xlSheet.Cells(1,1).Value = "22"
xlSheet.Cells(1,2).Value = "=a1*2"

# change sheet's name
xlSheet.Name = "ian" 

# INDEXING NOTE
xlSheet = xlBook.Sheets(1) # 1 based by default (but apparently can be changed)
xlSheet = xlBook.Sheets[1] # 0 based as per Python
# book recommends using () and using the docs where necessary - usually 1 is first item

#pyWin32 FAQ
#http://starship.python.net/crew/mhammond/win32/FAQ.html
#links to a tutorial which links to the MS docs
#http://www.boddie.org.uk/python/COM.html
#the above also have notes on 0/1 indexing 

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 233 (since July 30th)
  • Plays in last week: 0
  • Published: 79 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

7. anonymous Tue, 13 Jan 2009 07:54

Hello,

How to open an excel document protected by a password

Thank you for your help


Nice video. Very helpful!


Thank you. I look forward to learning more. This was quick and easy...straight to the point. I am want to learn more about "object oriented" methods. Not a computer science person, but I have seen how others can totally utilize this techniques.


Hi deeps, I'll mail you privately. You need Mark Hammond's pyWin32 which is linked in the description text below the video.

Ian.


hello ian

i m trying to follow ur instruction but i m getting a message no module named win32com.client

do i have to install something else with python or not.. i m not using ipython.. does this tutor work with simple python 2.5.

if u got time i want see some tutor about pdf tables in report lab.

thanks

look forward to hear from you thanks

deepak


Thanks - interesting topic


Video published, thanks for contributing to ShowMeDo


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com