I have an Excel file with one worksheet that has sediment collection data. I am running a long Python script.
In the worksheet is a column titled “CollectionYear.” Say I want the year 2010. If the year 2010 exists in the “CollectionYear” column, I want the rest of the script to run, if not then I want the script to stop.
This seems like an easy enough task but for the life of me I cannot figure it out nor find any examples.
Any help would be greatly appreciated.
I use xlrd all the time and it works great for me. Something like this might be helpful
I hope this points you in the right direction. More help could be given if the details of your problem were known.
Try using
xlwings
library to interface with Excel from pythonexample from their docs:
Here is what I learned from tackling a needle-in-a-haystack problem for a gigantic pile of
.xls
files. There are some thingsxlrd
and friends can't (or won't) do, such as getting the formula of a cell. For that, you'll need to use the Microsoft Component Object Model (COM)1.I recommend you find yourself a copy of Python Programming on Win32 by Mark Hammond. It's still useful 20 years later. Python Programming on Win32 covers the basics of the COM and how to access it using the
pywin32
library (also from Mark Hammond).In a nutshell, you can think of the COM as an API between a server (say, Excel) and a client (such as a Python script)2.
The COM API is reasonably well documented. Once you get used to the terminology, things become straight-forward albeit tedious. For example, an Excel file is technically a "Workbook". The "Workbooks" COM object has the Open method which provides a handle for Python to interact with the "Workbook". (Did you notice the different 's' endings on those?)
A "Workbook" contains a "Sheet", accessed here through the "Sheets" COM object:
Finally, the 'Cells' property of a worksheet "returns a Range object that represents all the cells on the worksheet". The Range object then has a Find method which will search within the range. The LookIn parameter allows for searching cell values, formulas, and comments.
The result of Find is a Range object which has many useful properties, like Formula, GetAddress, Value, and Text. You'll also find, as with anything Microsoft, that it's good enough for government work.
Finally, don't forget to close the workbook and to quit Excel!
You can extend these ideas with
Sheets.Item
andSheets.Count
and iterate over all sheets in a workbook (or all workbooks in a directory). You can have lots of fun!The headaches you may encounter include VBA macros and embedded objects, as well as the various different alerts each can produce. Performance is also an issue. The following silence notifications and can dramatically improve performance:
Application
Workbook
Another potential issue is late/early binding. Basically, does Python have information about the COM object? This affects things like introspection and how COM objects are referenced. The
win32com.client
package uses late-bound automation by default.With late-bound automation, Python doesn't know much about the COM object:
With early-bound automation, Python has full knowledge of the object:
To enable early binding, you must run
makepy.py
which is included withpywin32
. Runningmakepy.py
will prompt for the library to bind with.The process creates a Python file (in
Temp\
) which maps the methods and properties of the COM object.Early binding also provides access to COM constants, such as
msoAutomationSecurityForceDisable
andxlAscending
and is case-sensitive (whereas late-binding is not).That should be enough info to implement a Python-to-Excel library (like
xlwings
), overkill notwithstanding.1 Actually,
xlwings
works by utilizing the COM thoughpywin32
. Here's to one less dependency!2 This example uses
win32com.client.Dispatch
which requires processing happen through a single Excel instance. Usewin32com.client.DispatchEx
to create separate instances of Excel.