How To Read Excel Files In Python?

Discussion in 'Python' started by Anand, Dec 13, 2005.

  1. Anand

    Anand Guest

    Hello,

    Can I get some help on how to read the excel files using python?

    from win32com.client import Dispatch
    xlApp = Dispatch("Excel.Application")
    xlWb = xlApp.Workbooks.Open("Read.xls")
    xlSht = xlWb.WorkSheets(1)

    But sadly, I am unable to proceed further about how to read the cells of the
    worksheet of my excel file!

    Thanks for your help and cooperation.

    Best regards,
    Anand
    Anand, Dec 13, 2005
    #1
    1. Advertising

  2. Anand schrieb:
    > Hello,
    >
    > Can I get some help on how to read the excel files using python?
    > [...]


    Besides using the Excel component you could use the pyExcelerator
    <http://sourceforge.net/projects/pyexcelerator> module.
    You even don't need Windows for it.


    Bye,
    Dennis
    Dennis Benzinger, Dec 13, 2005
    #2
    1. Advertising

  3. Anand

    Steve Holden Guest

    Anand wrote:
    > Hello,
    >
    > Can I get some help on how to read the excel files using python?
    >
    > from win32com.client import Dispatch
    > xlApp = Dispatch("Excel.Application")
    > xlWb = xlApp.Workbooks.Open("Read.xls")
    > xlSht = xlWb.WorkSheets(1)
    >
    > But sadly, I am unable to proceed further about how to read the cells of the
    > worksheet of my excel file!
    >
    > Thanks for your help and cooperation.
    >
    > Best regards,
    > Anand
    >
    >

    The best way to proceed is to record actions as macros in Excel and then
    use the recorded VBA as a guide to the Pythin required.

    Unfortunately the office APIs aren't very complete in their documentation,

    Good places to look, as long as you don't mind rooting around:

    http://msdn.microsoft.com/office/understanding/excel/default.aspx

    http://msdn.microsoft.com/office/understanding/excel/documentation/default.aspx

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
    Steve Holden, Dec 13, 2005
    #3
  4. Anand

    gene tani Guest

    Anand wrote:
    > Hello,
    >
    > Can I get some help on how to read the excel files using python?
    >
    > from win32com.client import Dispatch
    > xlApp = Dispatch("Excel.Application")
    > xlWb = xlApp.Workbooks.Open("Read.xls")
    > xlSht = xlWb.WorkSheets(1)
    >
    > But sadly, I am unable to proceed further about how to read the cells of the
    > worksheet of my excel file!
    >
    > Thanks for your help and cooperation.
    >
    > Best regards,
    > Anand


    http://www.python.org/pypi/xlrd/0.3a1
    and the online (activestate) cookbook has lots of hits, search on
    "excel" "MS office" "CSV" "ODBC" etc
    http://aspn.activestate.com/ASPN/search/searchRecipes?query=excel
    gene tani, Dec 13, 2005
    #4
  5. Hi!


    I had few modif. your code :

    import time
    from win32com.client import Dispatch
    xlApp = Dispatch("Excel.Application")
    xlApp.Visible=True
    xlWb = xlApp.Workbooks.Open("Read.xls")
    print "D3:",xlWb.ActiveSheet.Cells(3,4).Value
    time.sleep(2)
    xlWb.Close(SaveChanges=0)
    xlApp.Quit()

    This run OK on my computers.



    @-salutations

    Michel Claveau
    Do Re Mi chel La Si Do, Dec 13, 2005
    #5
  6. Anand

    Steve M Guest

    """Derived from _Python Programming on Win32_ by Mark Hammond and Andy
    Robinson"""

    import win32com.client
    import win32com.client.dynamic

    class Excel:
    def __init__(self, filename=None):
    self.xlApp =
    win32com.client.dynamic.Dispatch('Excel.Application')
    if filename:
    self.filename = filename
    self.xlBook = self.xlApp.Workbooks.Open(filename)
    else:
    self.xlBook = self.xlApp.Workbooks.Add()
    self.filename = ''

    def save(self, newfilename=None):
    if newfilename:
    self.filename = newfilename
    self.xlBook.SaveAs(newfilename)
    else:
    self.xlBook.Save()

    def close(self):
    self.xlBook.Close(SaveChanges=0)
    del self.xlApp

    def show(self):
    self.xlApp.Visible = 1

    def hide(self):
    self.xlApp.Visible = 0

    def get_cell(self, sheet, row, col):
    "get value of one cell"
    sht = self.xlBook.Worksheets(sheet)
    return sht.Cells(row, col).Value

    def set_cell(self, sheet, row, col, value):
    "set value of one cell"
    sht = self.xlBook.Worksheets(sheet)
    sht.Cells(row, col).Value = value

    def get_range(self, sheet, row1, col1, row2, col2):
    "return a 2d array (i.e. tuple of tuples)"
    sht = self.xlBook.Worksheets(sheet)
    return sht.Range(sht.Cells(row1, col1), sht.Cells(row2,
    col2)).Value

    def set_range(self, sheet, leftCol, topRow, data):
    bottomRow = topRow + len(data) - 1
    rightCol = leftCol + len(data[0]) - 1
    sht = self.xlBook.Worksheets(sheet)
    sht.Range(sht.Cells(topRow, leftCol), sht.Cells(bottomRow,
    rightCol)).Value = data
    Steve M, Dec 13, 2005
    #6
  7. Anand

    John Machin Guest

    An alternative: the xlrd module. Don't need Excel on your machine,
    don't even need Windows. Pure Python. Happily handles large files
    (e.g. 120 Mb). Good date support.

    See http://www.lexicon.net/sjmachin/xlrd.htm or look for xlrd in the
    Cheese Shop.
    John Machin, Dec 15, 2005
    #7
  8. Anand

    alanwilhelm

    Joined:
    Jul 17, 2010
    Messages:
    1
    Tutorial w/code on parsing Excel docs with Python

    Hello, I put a tutorial together that might be of interest to you. Check it out at blog.ajwilhelm.net/archives/7

    Thanks!
    AJ
    alanwilhelm, Jul 17, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    937
    M.E.Farmer
    Feb 13, 2005
  2. Tim Golden
    Replies:
    0
    Views:
    693
    Tim Golden
    Dec 13, 2005
  3. sajithkahawatta

    read a excel sheet using aspose.excel

    sajithkahawatta, Apr 19, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    524
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Apr 19, 2007
  4. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,484
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
Loading...

Share This Page