zoukankan      html  css  js  c++  java
  • Python读写Excel文件

     先判断是否安装了xlrd库:pip install xlrd

    import xlrd
    # 给出excel文件绝对路径
    loc = ("C:\统计.xlsx") 
    # 打开工作表
    wb = xlrd.open_workbook(loc) 
    # 这里读取的是第一个sheet
    sheet = wb.sheet_by_index(0)
    #print(sheet.cell_value(2, 0) )
    #print("行数:")
    #print(sheet.nrows)
    #print("列数:")
    #print(sheet.ncols)
    for i in range(sheet.ncols):         
            print(sheet.cell_value(3, i)) 

    另一个库是:pip install xlwt

    import xlwt 
    from xlwt import Workbook
    # 创建workbook
    wb = Workbook() 
      
    # 使用add_sheet函数创建新的sheet
    sheet1 = wb.add_sheet('Sheet 1') 
    # 写入数据,参数分别为行、列、数据 
    sheet1.write(1, 0, 'ISBT DEHRADUN') 
    sheet1.write(2, 0, 'SHASTRADHARA') 
    sheet1.write(3, 0, 'CLEMEN TOWN') 
    sheet1.write(4, 0, 'RAJPUR ROAD') 
    sheet1.write(5, 0, 'CLOCK TOWER') 
    sheet1.write(0, 1, 'ISBT DEHRADUN') 
    sheet1.write(0, 2, 'SHASTRADHARA') 
    sheet1.write(0, 3, 'CLEMEN TOWN') 
    sheet1.write(0, 4, 'RAJPUR ROAD') 
    sheet1.write(0, 5, 'CLOCK TOWER') 
    sheet1.write(0, 6, '合計する') 
    # 保存到excel表格
    wb.save('C:\Users\E2-Li.y\Desktop\Java统计.xls')

    加粗:

    # 创建workbook
    workbook = xlwt.Workbook()  
    # 创建sheet 
    sheet = workbook.add_sheet("Sheet Name") 
    # 给单元格内容添加格式:加粗、标红
    style = xlwt.easyxf('font: bold 1,color: red') 
    # 在单元格中写入数据
    sheet.write(0, 0, 'SAMPLE', style) 
    # 保存excel
    workbook.save("sample.xls")
  • 相关阅读:
    3.30 DOM操作
    3.29 js例题
    3.28 函数
    3.27 数组例题
    Web 条件查询、分页查
    web页面增、删、改
    JDBC事务、下拉框
    JSTL、断点、JavaEE、DBUTils连接池
    jsp、el表达式
    Session技术 、jsp页面
  • 原文地址:https://www.cnblogs.com/shiningleo007/p/13679700.html
Copyright © 2011-2022 走看看