从 excel - Python - xlrd 读取命名范围
Reading a named range from excel - Python - xlrd
以下是我写的一段代码,超出阅读范围我无法继续。我希望能够读取范围的实际内容。感谢任何帮助。
import xlrd
xlBook = xlrd.open_workbook('data.xlsx')
# Open the sheet
sht = xlBook.sheet_by_name('calc')
# Look at the named range
named_obj = xlBook.name_map['load'][0]
# Now, able to retrieve the range
rangeData = (named_obj.formula_text)
(sheetName,ref) = rangeData.split('!')
# Gives me the range as $A:$B
print(ref)
# How do I print the contents of the cells knowing the range.
xlrd、xlwt、xlutils 根据其文档用于 .xls
文件。它建议对 .xlsx
个文件使用 openpyxl。然后你可以使用这个:
Read values from named ranges with openpyxl
我的方法是找出他的柱坐标,
但我仍然建议使用 openpyxl
更直观。
def col2int(s: str):
weight = 1
n = 0
list_s = list(s)
while list_s:
n += (ord(list_s.pop()) - ord('A')+1) * weight
weight *= 26
return n
# ...
# How do I print the contents of the cells knowing the range. ↓
temp, col_start, row_start, col_end, row_end = ref.replace(':', '').split('$')
for row in range(int(row_start)-1, int(row_end)):
for col in range(col2int(col_start)-1, col2int(col_end)):
print(sht.cell(row, col).value)
以下是我写的一段代码,超出阅读范围我无法继续。我希望能够读取范围的实际内容。感谢任何帮助。
import xlrd
xlBook = xlrd.open_workbook('data.xlsx')
# Open the sheet
sht = xlBook.sheet_by_name('calc')
# Look at the named range
named_obj = xlBook.name_map['load'][0]
# Now, able to retrieve the range
rangeData = (named_obj.formula_text)
(sheetName,ref) = rangeData.split('!')
# Gives me the range as $A:$B
print(ref)
# How do I print the contents of the cells knowing the range.
xlrd、xlwt、xlutils 根据其文档用于 .xls
文件。它建议对 .xlsx
个文件使用 openpyxl。然后你可以使用这个:
Read values from named ranges with openpyxl
我的方法是找出他的柱坐标,
但我仍然建议使用 openpyxl
更直观。
def col2int(s: str):
weight = 1
n = 0
list_s = list(s)
while list_s:
n += (ord(list_s.pop()) - ord('A')+1) * weight
weight *= 26
return n
# ...
# How do I print the contents of the cells knowing the range. ↓
temp, col_start, row_start, col_end, row_end = ref.replace(':', '').split('$')
for row in range(int(row_start)-1, int(row_end)):
for col in range(col2int(col_start)-1, col2int(col_end)):
print(sht.cell(row, col).value)