Python XLRD 使用范围 [英] Python XLRD use Range
本文介绍了Python XLRD 使用范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为一个变量分配一个excel范围:
I want to assign an excel range to a variable:
import xlrd
file = r"C:\Users\Lisa\Desktop\Frank\export.XLSX"
book = xlrd.open_workbook(file)
sheet = book.sheet_by_index(0)
data = [range("A3:D7")]
,但出现错误:
data = [range("A3:D7")]
TypeError: 'str' object cannot be interpreted as an integer
有什么想法吗?
推荐答案
您可以使用以下内容从 XLSX 文件中提取块,如下所示:
You could use the following to extract a block from an XLSX file as follows:
import xlrd
workbook = xlrd.open_workbook(r"input.xlsx")
sheet = workbook.sheet_by_index(0)
def get_cell_range(start_col, start_row, end_col, end_row):
return [sheet.row_slice(row, start_colx=start_col, end_colx=end_col+1) for row in xrange(start_row, end_row+1)]
data = get_cell_range(0, 2, 3, 6) # A3 to D7
print data
xlrd
使用从 0
开始的列号和行号而不是 Excel 单元格名称.它只能使用 xlrd.cellname()
将 Excel 单元格引用从 col row 格式转换为 A1
格式,而不是相反.
xlrd
works using column and row numbers starting from 0
instead of Excel cell names. It can only convert Excel cell references from col row format to A1
format using xlrd.cellname()
, not the other way around.
这篇关于Python XLRD 使用范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文