Return 范围内所有单元格地址的列表
Return list of all cell addresses within a Range
我在列表(例如 rng_list = ['$A:$A', '$B:$B', '$C:$C']
)中有一个范围列表(通过 openpyxl 从 Excel 工作簿加载),我想 "unpack" 每个范围到列表列表中的单独列表(即 unpacked_list = [['$A','$A','$A'], ['$B','$B','$B'], ['$C','$C','$C']]
)。
请参阅下面的代码,了解我目前在 Jupyter Notebook 中尝试过的内容。关于为什么我会收到以下错误的任何想法?或者,如果您对我可能想如何从不同角度处理这个问题有想法,那将不胜感激!谢谢!
import os
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
# create temp worksheet
wb_A = Workbook()
sheet_A = wb_A.create_sheet('sheetA')
# list with Excel ranges as str items in list
rng_list = ['$A:$B', '$C:$D', '$E:$F']
temp_list = []
unpacked_list = []
for item in rng_list:
for row in sheet_A(item): # use range from item in rng_list to iterate
through range in temp worksheet
for cell in row:
x = cell.row
y = cell.column
addr = get_column_letter(y) + str(x)
temp_list.append(addr)
unpacked_list.append(addr)
# delete temp worksheet
wb_A.remove(sheet_A)
unpacked_list
我希望使用列表中的范围 str 来遍历 "dummy worksheet" 创建的只是为了遍历单元格范围并捕获范围内相应的单元格地址。我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-85-13b28d369550> in <module>
14
15 for item in rng_list:
---> 16 for row in sheet_A(item): # use range from item in rng_list to iterate through range in temp worksheet
17 for cell in row:
18 x = cell.row
TypeError: 'Worksheet' object is not callable
语法!要访问您需要使用方括号的单元格,括号用于函数调用
应该是sheet_A[item]
更正原始代码中的语法错误后(感谢 Rahasya Prabhakar!),我修改了原始代码以根据需要工作。
具体来说,我需要在初始 For 循环的开头将“'temp_list'”重新定义为一个空列表,并在末尾附加到“'unpacked_list'”初始 For 循环的一部分,以根据需要获取解压缩范围列表的列表。
'''
导入 os
从 openpyxl 导入工作簿
从 openpyxl.utils 导入 get_column_letter
# create temp worksheet
wb_A = Workbook()
sheet_A = wb_A.create_sheet('sheetA')
# list with Excel ranges as str items in list
rng_list = ['$A:$B', '$C:$D', '$E:$F']
temp_list = []
unpacked_list = []
for item in rng_list:
temp_list=[]
for row in sheet_A[item]: # use range from item in rng_list to iterate through
range in temp worksheet
for cell in row:
x = cell.row
y = cell.column
addr = get_column_letter(y) + str(x)
temp_list.append(addr)
unpacked_list.append(temp_list)
# delete temp worksheet
wb_A.remove(sheet_A)
print(unpacked_list)
'''
我在列表(例如 rng_list = ['$A:$A', '$B:$B', '$C:$C']
)中有一个范围列表(通过 openpyxl 从 Excel 工作簿加载),我想 "unpack" 每个范围到列表列表中的单独列表(即 unpacked_list = [['$A','$A','$A'], ['$B','$B','$B'], ['$C','$C','$C']]
)。
请参阅下面的代码,了解我目前在 Jupyter Notebook 中尝试过的内容。关于为什么我会收到以下错误的任何想法?或者,如果您对我可能想如何从不同角度处理这个问题有想法,那将不胜感激!谢谢!
import os
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
# create temp worksheet
wb_A = Workbook()
sheet_A = wb_A.create_sheet('sheetA')
# list with Excel ranges as str items in list
rng_list = ['$A:$B', '$C:$D', '$E:$F']
temp_list = []
unpacked_list = []
for item in rng_list:
for row in sheet_A(item): # use range from item in rng_list to iterate
through range in temp worksheet
for cell in row:
x = cell.row
y = cell.column
addr = get_column_letter(y) + str(x)
temp_list.append(addr)
unpacked_list.append(addr)
# delete temp worksheet
wb_A.remove(sheet_A)
unpacked_list
我希望使用列表中的范围 str 来遍历 "dummy worksheet" 创建的只是为了遍历单元格范围并捕获范围内相应的单元格地址。我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-85-13b28d369550> in <module>
14
15 for item in rng_list:
---> 16 for row in sheet_A(item): # use range from item in rng_list to iterate through range in temp worksheet
17 for cell in row:
18 x = cell.row
TypeError: 'Worksheet' object is not callable
语法!要访问您需要使用方括号的单元格,括号用于函数调用
应该是sheet_A[item]
更正原始代码中的语法错误后(感谢 Rahasya Prabhakar!),我修改了原始代码以根据需要工作。
具体来说,我需要在初始 For 循环的开头将“'temp_list'”重新定义为一个空列表,并在末尾附加到“'unpacked_list'”初始 For 循环的一部分,以根据需要获取解压缩范围列表的列表。
''' 导入 os 从 openpyxl 导入工作簿 从 openpyxl.utils 导入 get_column_letter
# create temp worksheet
wb_A = Workbook()
sheet_A = wb_A.create_sheet('sheetA')
# list with Excel ranges as str items in list
rng_list = ['$A:$B', '$C:$D', '$E:$F']
temp_list = []
unpacked_list = []
for item in rng_list:
temp_list=[]
for row in sheet_A[item]: # use range from item in rng_list to iterate through
range in temp worksheet
for cell in row:
x = cell.row
y = cell.column
addr = get_column_letter(y) + str(x)
temp_list.append(addr)
unpacked_list.append(temp_list)
# delete temp worksheet
wb_A.remove(sheet_A)
print(unpacked_list)
'''