Hey guys,

Just as the title says, I want to implement using an excel spreadsheet as an argument for a python script. I already have the layout of the spreadsheet, but I want to be able to assign the variables in my python script from specific cells of the spreadsheet.

So, ideally I want to be able to type something like "script.py spreadsheet" in my cygwin command prompt.

Thanks,

kayoh

Recommended Answers

All 3 Replies

You only need to read the spreadsheet file in sys.argv and use the module xlrd

#!/usr/bin/env python
import sys
import xlrd

if __name__ == "__main__":
    assert len(sys.argv) == 2
    spreadsheet = sys.argv[-1]
    wb = xlrd.open_workbook(spreadsheet)
    sh = wb.sheet_by_index(0)
    my_variable = sh.cell_value(0,0) # access a cell

Get xlrd from pypi with the easy_install command.

Thanks for the prompt response, however I'm having problems installing the xlrd module, could provide more details regarding the "easy_install" command?

I went here: http://pypi.python.org/pypi/xlrd and downloaded the MS Windows Installer, when I ran it it found where my python was installed but when I tried to import it into my script the module wasn't found.

Thanks for the prompt response, however I'm having problems installing the xlrd module, could provide more details regarding the "easy_install" command?

I went here: http://pypi.python.org/pypi/xlrd and downloaded the MS Windows Installer, when I ran it it found where my python was installed but when I tried to import it into my script the module wasn't found.

You are probably using the cygwin version of python. To have easy_install, download setuptools as described here
http://pypi.python.org/pypi/setuptools#cygwin-mac-os-x-linux-other (the file to download is near the end of the web page). Once setuptools is installed, you can install xlrd (as well as 15000 other modules in pypi) by running easy_install xlrd on the cygwin command line (when a working internet connection is available).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.