<p>阅读<a href="http://openpyxl.readthedocs.io" rel="noreferrer">OpenPyXL Documentation</a></p>
<p>在<code>workbook</code>中对所有<code>worksheets</code>进行迭代,例如:</p>
<pre><code>for n, sheet in enumerate(wb.worksheets):
print('Sheet Index:[{}], Title:{}'.format(n, sheet.title))
</code></pre>
<blockquote>
<p><strong>Output</strong>: </p>
<pre><code>Sheet Index:[0], Title: Sheet
Sheet Index:[1], Title: Sheet1
Sheet Index:[2], Title: Sheet2
</code></pre>
</blockquote>
<hr/>
<p>在<strong>一个工作表中对所有<code>rows</code>和<code>columns</code>进行迭代:</p>
<pre><code>worksheet = workbook.get_sheet_by_name('Sheet')
for row_cells in worksheet.iter_rows():
for cell in row_cells:
print('%s: cell.value=%s' % (cell, cell.value) )
</code></pre>
<p><strong>输出</strong>:</p>
<pre><code><Cell Sheet.A1>: cell.value=²234
<Cell Sheet.B1>: cell.value=12.5
<Cell Sheet.C1>: cell.value=C1
<Cell Sheet.D1>: cell.value=D1
<Cell Sheet.A2>: cell.value=1234
<Cell Sheet.B2>: cell.value=8.2
<Cell Sheet.C2>: cell.value=C2
<Cell Sheet.D2>: cell.value=D2
</code></pre>
<hr/>
<p>在<strong>一个</strong><code>row</code>的所有<code>columns</code>上迭代,例如<code>row==2</code>:</p>
<pre><code>for row_cells in worksheet.iter_rows(min_row=2, max_row=2):
for cell in row_cells:
print('%s: cell.value=%s' % (cell, cell.value) )
</code></pre>
<p><strong>输出</strong>:</p>
<pre><code><Cell Sheet.A2>: cell.value=1234
<Cell Sheet.B2>: cell.value=8.2
<Cell Sheet.C2>: cell.value=C2
<Cell Sheet.D2>: cell.value=D2
</code></pre>
<hr/>
<p>全部迭代</strong><code>rows</code>,仅<code>column</code><strong>2:</p>
<pre><code>for col_cells in worksheet.iter_cols(min_col=2, max_col=2):
for cell in col_cells:
print('%s: cell.value=%s' % (cell, cell.value))
</code></pre>
<p><strong>输出</strong>:</p>
<pre><code><Cell Sheet.B1>: cell.value=12.5
<Cell Sheet.B2>: cell.value=8.2
<Cell Sheet.B3>: cell.value=9.8
<Cell Sheet.B4>: cell.value=10.1
<Cell Sheet.B5>: cell.value=7.7
</code></pre>
<p><strong><em>使用Python测试:3.4.2-openpyxl:2.4.1-LibreOffice:4.3.3.2</em></strong></p>