zoukankan      html  css  js  c++  java
  • python处理excel

    xlrd, xlwt 用来处理xls,支持Excel2003版,pyexcel处理xlsx,支持excel2007版
    我这里我用xlrd,xlwt来生成excel,可能会存在不兼容,但是功能没有问题

    用的python3,因为python2会有乱码问题。
    # -*- coding: utf-8 -*-
    import xlrd, xlwt
    
    
    def read():
        data = xlrd.open_workbook("D:\pyproject\python_excel\1.xlsx")  #打開已存在的excel
        # data.sheets()
        tables = data.sheet_names()             #获取sheet表名字
    
        for table in tables:
            data1 = xlwt.Workbook()
            sheet1 = data1.add_sheet('汇总', cell_overwrite_ok=True) #写入excel
    
            sheet = data.sheet_by_name(table)
            print(table)
    
            a = sheet.col_values(1, 1)                    #读取对应列的数据
            b = sheet.col_values(4, 1)
            c = sheet.col_values(7, 1)
            print(a, b, c)
            for i in range(0, len(a)):
                sheet1.write(i, 0, a[i])                 #在excel中写入对应位置
                sheet1.write(i, 1, b[i])
                sheet1.write(i, 2, c[i])
    
    
            data1.save('D:\pyproject\python_excel\2.xls')
    
    
    if __name__ == '__main__':
        read()
  • 相关阅读:
    yii中通过HTTP post接收
    网络编程
    python 异常处理
    面向对象(2)
    面向对象
    什么是模块精讲
    常用模块二
    各种推导式详解
    匿名函数
    迭代器生成器
  • 原文地址:https://www.cnblogs.com/jinjidedale/p/8929936.html
Copyright © 2011-2022 走看看