第12章 电子表格单元格翻转程序

##第十二章 电子表格单元格翻转程序
##方法一:
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')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值