<p><em>我正在使用python xlrd<a href="http://scienceoss.com/read-excel-files-from-python/" rel="noreferrer">http://scienceoss.com/read-excel-files-from-python/</a>从excel工作表中读取数据</em></p>
<p>你需要考虑你在做什么,而不是抓取一些博客代码,留下一些完全不相关的东西,比如<code>wb.sheet_names()</code>,省略一些与你的需求非常相关的部分,比如<code>first_column = sh.col_values(0)</code>。</p>
<p>以下是如何查找A列(第一列)中最后一个“whatever”的行索引-未测试:</p>
<pre><code>import xlrd
wb = xlrd.open_workbook(file_name)
# Why do you think that you need to use encoding_overide?
sheet0 = wb.sheet_by_index(0)
tag = u"Employee name" # or u"Emp name" or ...
column_0_values = sheet0.col_values(colx=0)
try:
max_tag_row_index = column_0_values.rindex(tag)
print "last tag %r found at row_index %d" % (
tag, max_tag_row_index)
except IndexError:
print "tag %r not found" % tag
</code></pre>
<p>现在我们需要解释“<em>我们如何读取从第一个单元格中最后一行开始的最后一列</em>”</p>
<p>假设“最后一列”是指具有列_index==sheet0.ncols-1的列,则:</p>
<pre><code>last_colx = sheet0.ncols - 1
required_values = sheet0.col_values(colx=last_colx, start_rowx=max_tag_row_index)
required_cells = sheet0.col_slice(colx=last_colx, start_rowx=max_tag_row_index)
# choose one of the above 2 lines, depending on what you need to do
</code></pre>
<p>如果这不是你的意思(这是很可能的,因为它忽略了一大堆数据(为什么你只想读最后一列?),请试着用例子来解释你的意思。</p>
<p>可能需要在剩余的单元格上迭代:</p>
<pre><code>for rowx in xrange(max_tag_row_index, sheet0.nrows): # or max_tag_row_index + 1
for colx in xrange(0, sheet0.ncols):
do_something_with_cell_object(sheet0.cell(rowx, colx))
</code></pre>