다소 큰 .xlsx 파일이 있습니다. 19 열, 5185 행입니다. 파일을 열고 한 열의 모든 값을 읽고, 그 값에 몇 가지 작업을 수행 한 다음, 같은 통합 문서에 새 열을 만들고 수정 된 값을 쓰려고합니다. 따라서 동일한 파일에서 읽고 쓸 수 있어야합니다. 통합 문서가 너무 커서 때문에openpyxl의 읽기 전용 통합 문서의 열을 반복합니다.
def readExcel(doc):
wb = load_workbook(generalpath + exppath + doc)
ws = wb["Sheet1"]
# iterate through the columns to find the correct one
for col in ws.iter_cols(min_row=1, max_row=1):
for mycell in col:
if mycell.value == "PerceivedSound.RESP":
origCol = mycell.column
# get the column letter for the first empty column to output the new values
newCol = utils.get_column_letter(ws.max_column+1)
# iterate through the rows to get the value from the original column,
# do something to that value, and output it in the new column
for myrow in range(2, ws.max_row+1):
myrow = str(myrow)
# do some stuff to make the new value
cleanedResp = doStuff(ws[origCol + myrow].value)
ws[newCol + myrow] = cleanedResp
wb.save(doc)
그러나, 파이썬은 행 3853 후 메모리 오류가 발생했습니다 :
내 원래의 코드는이했다. openpyxl 문서는 큰 통합 문서를 처리하기 위해 읽기 전용 모드 (https://openpyxl.readthedocs.io/en/latest/optimized.html)를 사용한다고합니다. 나는 그것을 사용하려고하고있다. AttributeError :
def readExcel(doc):
wb = load_workbook(generalpath + exppath + doc, read_only=True)
ws = wb["Sheet1"]
for col in ws.iter_cols(min_row=1, max_row=1):
#etc.
파이썬이 오류가 발생합니다 : 그러나, 나는 READ_ONLY = 진정한 PARAM를 추가 할 때 열을 반복 할 수있는 방법은없는 것 같다 'ReadOnlyWorksheet'객체가 어떤 속성 'iter_cols 없다'
I가 위의 코드에서 마지막 줄을 변경할 경우 : 에서 :
for col in ws.columns:
파이썬 같은 오류가 발생합니다 tributeError는 'ReadOnlyWorksheet'개체가 행에 대해 아무런 속성 '열'
순회 괜찮있다 (및 설명서 제가 위에 링크에 포함되어 있습니다) :
for col in ws.rows:
(에러 없음)
This question은 AttritubeError에 대해 묻지 만 읽기 전용 모드를 제거하는 것이 해결책입니다. openpyxl은 읽기 전용 모드가 아닌 전체 통합 문서를 읽지 않기 때문에 저에게 적합하지 않습니다.
그래서 어떻게 대형 통합 문서의 열을 반복합니까?
아직이 문제가 발생하지 않았지만 한 번 열을 반복 할 수 있습니다. 통합 문서가 큰 경우 동일한 통합 문서를 읽고 쓰려면 어떻게해야합니까?
감사합니다.
다른 통합 문서간에 셀 개체를 전달할 수 없으므로 작동하지 않습니다. –
같은 줄을 serialize 할 수 있습니다 :'row = [cell.value for cell in row]'? – cowbert
확실히,하지만 이것은 서식, 주석 등을 잃어 버릴 것을 잊지 마십시오. –