python读取写入excle

import xlrd
import xlwt

##读取excle并存入列表
def get_ip():
    # 打开excel文件,创建一个workbook对象,book对象也就是fruits.xlsx文件,表含有sheet名
    rbook = xlrd.open_workbook('ip.xlsx')

    # sheets方法返回对象列表,[<xlrd.sheet.Sheet object at 0x103f147f0>]
    rbook.sheets()
    # xls默认有3个工作簿,Sheet1,Sheet2,Sheet3
    rsheet = rbook.sheet_by_index(0)  # 取第一个工作簿
    ip=rsheet.col_values(1)[1:]  #读取IP整列不要第一行
    #pw=rsheet.col_values(4)[1:]   #读取密码整列不要第一行
    #print('print',ip)
    return  ip

#写入excle
def wt():

    workbook = xlwt.Workbook()
    #新建sheet
    worksheet = workbook.add_sheet('test')
    #获取写的值的列表
    ip=get_ip()
    a = 1
    for  i in ip:
       worksheet.write(a, 0,i)  ##循环写入
       a+=1
    workbook.save('ip.xls')#写入的文件名

  

posted @ 2021-06-01 16:01  May_It_Be  阅读(101)  评论(0)    收藏  举报