python 操作excel


  1. 安装
pip install xlwt  # xlwt库负责将数据导入生成Excel表格文件.
pip install xlrd  # xlrd库则负责将Excel表格中的数据取出来。
  1. 使用样例
import xlwt
from datetime import datetime


def excel():
    wb = xlwt.Workbook()
    # 添加一个表
    ws = wb.add_sheet('cpsapi.lejunwl.com访问异常ip')

    now_time = datetime.now().strftime('%Y%m%d%H%M%S')
    # 3个参数分别为行号,列号,和内容
    # 需要注意的是行号和列号都是从0开始的
    ws.write(0, 0, 'id')
    ws.write(0, 1, 'server_ip')
    ws.write(0, 2, 'domain')
    ws.write(0, 3, 'request_ip')
    ws.write(0, 4, 'request_location')
    ws.write(0, 5, 'count_per_minute')
    ws.write(0, 6, 'start_time')
    ws.write(0, 7, 'end_time')
    ws.write(0, 8, 'uri_info')
    ws.write(0, 9, 'B账号')
    ws.write(0, 10, '对接人')
    ws.write(0, 11, '部门')
    ws.write(0, 12, '是否被封禁')
    wb.save('/Users/mac/Desktop/TMP/访问异常ip-%s.xls' % now_time)

if __name__ == '__main__':
     excel()