##第十二章 电子表格单元格翻转程序 ##方法一: import openpyxl from openpyxl.utils import get_column_letter wb = openpyxl.load_workbook('style.xlsx') sheet = wb.active # length = sheet.max_row if sheet.max_row > sheet.max_column else sheet.max_column length = [sheet.max_column,sheet.max_row][sheet.max_row > sheet.max_column]#取行跟列的较大值 for i in range(1, length+1): for j in range(1, i): temp =sheet[get_column_letter(i) + str(j)].value sheet[get_column_letter(i) + str(j)] = sheet[get_column_letter(j) + str(i)].value sheet[get_column_letter(j) + str(i)] = temp # print(str(sheet[get_column_letter(i) + str(j)].value)+' '+str(sheet[get_column_letter(j) + str(i)].value)) print("Done") wb.save('style11.xlsx') ##电子表格单元格翻转程序方法二 import openpyxl#导入模块 from openpyxl.utils import column_index_from_string wb=openpyxl.load_workbook('style.xlsx') wb2 = openpyxl.Workbook() sheet = wb.active sheet2 = wb2.active for rowobj in sheet.rows:#迭代每一个单元格 for cellobj in rowobj: # print(cellobj) sheet2.cell(row=column_index_from_string(cellobj.column),column=cellobj.row).value=cellobj.value wb2.save('style22.xlsx') print('Done')