2017-05-16 2 views
1

Excel에서 명명 된 범위/정의 된 이름의 셀을 반복하고 Python 2.7에서 openpyxl을 사용하여 명명 된 범위 내의 각 셀 값을 설정하려면 어떻게해야합니까?Python에서 openpyxl을 사용하여 Excel 명명 된 범위 사용

다음과 같은 내용이 발견되었지만 명명 된 범위 내에서 개별 셀의 값을 인쇄하고 설정하는 데 사용할 수 없습니다.

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

정확하게 이해하지 못하는 부분은 무엇입니까? –

+0

개별 셀 값 추출 중,하지만 지금 해결하고 다른 사람이 동일한 문제가있는 경우 솔루션을 게시합니다. 그것을 들여다 주셔서 감사합니다. – Py1001

답변

-1

나는 그것을 처리 할 수 ​​있었다. 아마도 다음은 openpyxl을 사용하여 정의 된 이름 또는 명명 된 범위의 각 셀 값에 액세스하려는 다른 사용자에게 유용 할 것입니다.

import openpyxl 

wb = openpyxl.load_workbook('filename.xlsx') 
#getting the address 
address = list(wb.defined_names['metrics'].destinations) 

#removing the $ from the address 
for sheetname, cellAddress in address: 
    cellAddress = cellAddress.replace('$','') 

#looping through each cell address, extracting it from the tuple and printing it out  
worksheet = wb[sheetname] 
for i in range(0,len(worksheet[cellAddress])): 
    for item in worksheet[cellAddress][i]: 
     print item.value` 
관련 문제