zoukankan      html  css  js  c++  java
  • Python学习笔记-EXCEL操作

    环境Python3

    创建EXCEL,覆盖性创建

    #conding=utf-8
    import xlwt
    
    def BuildExcel(ExcelName,SheetName,TitleList,DataList):
        workbook = xlwt.Workbook()
        sheet = workbook.add_sheet(SheetName)
        col=0
        for title in TitleList:
            sheet.write(0,col,title)
            col=col+1
        row=1
        for rows in DataList:
            col=0
            for column in rows:
                sheet.write(row,col,column)
                col=col+1
            row=row+1   
        workbook.save(ExcelName)
    
    ExcelName="未完成订单.xls"
    SheetName="Order"

    #标题 TitleList=['ID','订单号','更新情况']
    #内容 DataList
    =[(1,1000,1),(2,1001,0)] BuildExcel(ExcelName,SheetName,TitleList,DataList)

    读取EXCEL内容,返回标题列表,数据列表

    def ReadExcel(ExcelName,SheetName):
        workbook = xlrd.open_workbook(ExcelName)
        sheet = workbook.sheet_by_name(SheetName)
        TitleList=sheet.row_values(0)
        tn=len(TitleList)
        DataList=[]
        for i in range(1,sheet.nrows):
            """EXCEL第I行,第N列值:sheet.row_values(i)[N-1]"""
            RowList=[]
            for col in range(0,tn):
                RowList.append(str(sheet.row_values(i)[col]))
            DataList.append(tuple(RowList))
        return TitleList,DataList

    编辑EXCEL值,根据ChangeList变更单元格值

    #conding=utf-8
    import xlrd
    from xlutils.copy import copy
    #编辑EXCEL,ChangeList传入待变更的行号、列号以及值。
    #row=row-1,col=col-1,ChangeList=[(row,col,"变更值")]
    def EditExcel(ExcelName,ChangeList):
        workbook = xlrd.open_workbook(ExcelName)
        workbooknew = copy(workbook)
        sheet = workbooknew.get_sheet(0)
        for rows in ChangeList:     
            sheet.write(rows[0], rows[1], rows[2])
        workbooknew.save(ExcelName)
  • 相关阅读:
    [洛谷P2463][SDOI2008]Sandy的卡片
    后缀数组
    周记【距gdoi:110天】
    hdu3068最长回文(Manacher算法)
    二分图的一些题目合集
    周记【距gdoi:117天】
    二分图、网络流模版总结
    2014end
    周记【距gdoi:126天】
    2-sat
  • 原文地址:https://www.cnblogs.com/Evan-fanfan/p/8968093.html
Copyright © 2011-2022 走看看