如何在Excel命名范围/定义名称中循环遍历单元格,并使用带有Python 2.7的openpyxl将每个单元格值设置在指定范围内?
我发现了以下内容,但未设法将其用于打印并在指定范围内设置单个单元格的值。
Read values from named ranges with openpyxl
到目前为止,这是我的代码,我已经在评论中提出要进行更改的内容。谢谢你的期待。
#accessing a named range called 'metrics'
namedRange = loadedIndividualFile.defined_names['metrics']
#obtaining a generator of (worksheet title, cell range) tuples
generator = namedRange.destinations
#looping through the generator and getting worksheet title, cell range
cells = []
for worksheetTitle, cellRange in generator:
individualWorksheet = loadedIndividualFile[worksheetTitle]
#==============================
#How do I set cell values here?
# I am looking to print and change each cell value within the defined name range
#==============================
print cellRange
print worksheetTitle
#theWorksheet = workbook[worksheetTitle]
#cell = theWorksheet[cellRange]
答案 0 :(得分:3)
我设法解决了这个问题。对于希望使用openpyxl访问定义名称或命名范围中每个单元格值的其他人,以下内容可能会有用。
{{1}}