python 读取与写入excel

这段代码展示了如何使用openpyxl库在Python中创建和操作Excel文件。首先,创建了一个新的工作簿并获取了第一个工作表。接着,遍历工作表的特定列,收集值并进行排序。最后,找到最大值的索引及其对应的时间值。这是一个关于Python Excel操作和数据排序的示例。
摘要由CSDN通过智能技术生成

python write msg to the excel

def write_xls():
    f = openpyxl.Workbook()
    f.create_chartsheet()
    sheet_names = wb2.get_sheet_names()
    sheet_names = list(sheet_names)
    print(sheet_names[0])
    table = wb2.get_sheet_by_name(sheet_names[0])
    table.cell(row=1, column=1).value = '1'

下段代码主要是对excel某一列进行排序并查出该值对应的时间

import openpyxl
import copy
wb = openpyxl.load_workbook(filename="test,xlsx")
#get sheet name for the sheet table
#return value is a tuple
sheet_names = wb.get_sheet_names()
#transfer it to the list
sheet_names = list(sheet_names)
print(sheet_names[0])
table = wb.get_sheet_by_name(sheet_names[0])
#get the max column and the row
max_cols = table.max_column
max_rows = table.max_row
ar_column_requests = []
ar_column_requests1 = []
for i in range(max_rows):
        # print(table.cell(row=3+i, column=2).value)
        if table.cell(row=3+i, column=2).value:
            ar_column_requests.append(table.cell(row=3+i, column=2).value)
            ar_column_requests1.append(table.cell(row=3+i, column=1).value)
print(ar_column_requests)
#deep copy can malloc another space to store the array
ar_info_temp =  copy.deepcopy(ar_column_requests) 
ar_column_requests.sort(reverse=True)
max = ar_column_requests[0]
index_msg = ar_info_temp.index(max)
print(index_msg)
print(ar_column_requests1[index_msg])

the excel is as below
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值