<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Using Excel in Python</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/04/06/Using-Excel-in-Python</link>
      <pubDate>Sat, 06 Apr 2013 14:01:02 EDT</pubDate>
      <category><![CDATA[programming]]></category>
      <guid isPermaLink="false">DqsbRvB0NphgnxGWAK0Z8o1qnLU=</guid>
      <description>Using Excel in Python</description>
      <content:encoded><![CDATA[


&lt;p&gt;
There may be a time where you have an Excel sheet that already has a model built into it, and you normally change cells in the sheet, and it solves the model. It can be tedious to do that a lot, and we can use python to do that. Python has a COM interface that can communicate with Excel (and many other windows programs. see &lt;a href="http://my.safaribooksonline.com/1565926218" &gt;http://my.safaribooksonline.com/1565926218&lt;/a&gt; for Python Programming on Win32). In this example, we will use a very simple Excel sheet that calculates the volume of a CSTR that runs a zeroth order reaction (\(-r_A = k\)) for a particular conversion. You set the conversion in the cell B1, and the volume is automatically computed in cell B6. We simply need to set the value of B1, and get the value of B6 for a range of different conversion values. In this example, the volume is returned in Liters.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; win32com.client &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; win32
excel = win32.Dispatch(&lt;span style="color: #228b22;"&gt;'Excel.Application'&lt;/span&gt;)

wb = excel.Workbooks.Open(&lt;span style="color: #228b22;"&gt;'c:/Users/jkitchin/Dropbox/pycse/data/cstr-zeroth-order.xlsx'&lt;/span&gt;)
ws = wb.Worksheets(&lt;span style="color: #228b22;"&gt;'Sheet1'&lt;/span&gt;)

X = [0.1, 0.5, 0.9]
&lt;span style="color: #8b0000;"&gt;for&lt;/span&gt; x &lt;span style="color: #8b0000;"&gt;in&lt;/span&gt; X:
    ws.Range(&lt;span style="color: #228b22;"&gt;"B1"&lt;/span&gt;).Value = x
    V = ws.Range(&lt;span style="color: #228b22;"&gt;"B6"&lt;/span&gt;).Value
    &lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; &lt;span style="color: #228b22;"&gt;'at X = {0} V = {1:1.2f} L'&lt;/span&gt;.format(x, V)
 
&lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;we tell Excel the workbook is saved, even though it is not, so it&lt;/span&gt;
&lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;will quit without asking us to save.&lt;/span&gt;
excel.ActiveWorkbook.Saved = &lt;span style="color: #8b0000;"&gt;True&lt;/span&gt; 
excel.Application.Quit()
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
at X = 0.1 V = 22.73 L
at X = 0.5 V = 113.64 L
at X = 0.9 V = 204.55 L
&lt;/pre&gt;

&lt;p&gt;
This was a simple example (one that did not actually need Excel at all) that illustrates the feasibility of communicating with Excel via a COM interface. 
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2013 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2013/04/06/Using-Excel-in-Python.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
