zoukankan      html  css  js  c++  java
  • 如何读写excel文件?

    需求:
    Microsoft Excel是目前办公中使用最频繁的软件,其数据格式为xls,xlsx,一种非常常用的电子表格,小学某班成绩,记录在excel文件中:
    姓名 语文 数学 外语
    李雷 95 99 96
    韩梅 98 100 93
    张峰 94 95 95
    ...
    利用python读写excel,添加'总分'列,计算每人总分。

    思路:
    使用pip安装 pip install xlrd xlwt
    使用第三方库xlrd和xlwt,这两个库分别用于excel的读写

    代码:

    import xlrd,xlwt
    
    rbook = xlrd.open_workbook('shizhanscore.xlsx')  # 创建一个读的工作薄对象
    rsheet = rbook.sheet_by_index(0) # 读取工作薄中的第一张表
    
    nc = rsheet.ncols # 要增加的新列的列坐标
    
    rsheet.put_cell(0,nc,xlrd.XL_CELL_TEXT,'总分',None)  # 添加新列并对新列的内容属性进行定义
    
    # 进行迭代计算总分
    for row in range(1,rsheet.nrows):
        t = sum(rsheet.row_values(row,1))  # 获取一行的值返回一个列表,从1列开始,即却除了姓名,并进行求和
        rsheet.put_cell(row,nc,xlrd.XL_CELL_NUMBER,t,None)  # 将计算的结果存入单元格
         
    
    wbook = xlwt.Workbook()  # 创建一个写的工作簿的对象
    wsheet = wbook.add_sheet(rsheet.name)  # 添加一张表,与上面rsheet的相同
    style = xlwt.easyxf('align:vertical center, horizontal center')  # 水平居中和垂直居中
    
    # 遍历上面rsheet表的每一个单元格进行写入
    for r in range(rsheet.nrows):
        for c in range(rsheet.ncols):
            wsheet.write(r,c,rsheet.cell_value(r,c),style)
    
    wbook.save('output.xls')  # 结果进行保存
    
    ==============================================================================
    
    >>> import xlrd
    
    >>> book = xlrd.open_workbook('demo.xlsx')
    
    >>> book
    <xlrd.book.Book at 0x7f91cb81b908>
    
    >>> book.sheets()
    [<xlrd.sheet.Sheet at 0x7f91cb600518>]
    
    >>> book.sheet_by_index(0)
    <xlrd.sheet.Sheet at 0x7f91cb600518>
    
    >>> sheet = book.sheet_by_index(0)
    
    >>> sheet.nrows
    8
    
    >>> sheet.ncols
    4
    
    >>> c00 = sheet.cell(0,0)
    
    >>> c00
    text:'姓名'
    
    >>> type(c00)
    xlrd.sheet.Cell
    
    >>> c00.ctype
    1
    
    >>> c00.value
    '姓名'
    
    >>> xlrd.XL_CELL_TEXT
    1
    
    >>> xlrd.XL_CELL_NUMBER
    2
    
    >>> c11 = sheet.cell(1,1)
    
    >>> c11.ctype
    2
    
    >>> c11.value
    34.0
    
    >>> sheet.cell_value(1,1)
    34.0
    
    >>> sheet.cell_value(0,0)
    '姓名'
    
    >>> sheet.row(0)
    [text:'姓名', text:'语文', text:'数学', text:'英语']
    
    >>> sheet.row_values(0)
    ['姓名', '语文', '数学', '英语']
    
    >>> sheet.row_values?
    Signature: sheet.row_values(rowx, start_colx=0, end_colx=None)
    Docstring: Returns a slice of the values of the cells in the given row.
    File:      ~/.local/lib/python3.6/site-packages/xlrd/sheet.py
    Type:      method
    
    >>> sheet.row_values(1,1)
    [34.0, 56.0, 89.0]
    
    >>> sheet.put_cell?
    Signature: sheet.put_cell(rowx, colx, ctype, value, xf_index)
    Docstring: <no docstring>
    File:      ~/.local/lib/python3.6/site-packages/xlrd/sheet.py
    Type:      method
    
    >>> sheet.put_cell(0,sheet.ncols,xlrd.XL_CELL_TEXT,'总分'None)
      File "<ipython-input-36-53276b64c686>", line 1
        sheet.put_cell(0,sheet.ncols,xlrd.XL_CELL_TEXT,'总分'None)
                                                              ^
    SyntaxError: invalid syntax
    
    
    >>> sheet.put_cell(0,sheet.ncols,xlrd.XL_CELL_TEXT,'总分',None)
    
    >>> import xlwt
    
    >>> wbook = xlwt.Workbook()
    
    >>> wsheet = wbook.add_sheet('test')
    
    >>> wsheet.write?
    
    >>> wsheet.write(0,0,'abc')
    
    >>> wsheet.write(0,1,100)
    
    >>> wbook.save('test.xlsx')
    
    >>> 
    ============================================================================
    import xlrd,xlwt
    
    rbook =  xlrd.open_workbook('demo.xlsx')
    rsheet = rbook.sheet_by_index(0)
    k = rsheet.ncols
    rsheet.put_cell(0,k,xlrd.XL_CELL_TEXT,'总分',None)
    
    for i in range(1,rsheet.nrows):
        t = sum(rsheet.row_values(i,1))
        rsheet.put_cell(i,k,xlrd.XL_CELL_NUMBER,t,None)
    
    wbook = xlwt.Workbook()
    wsheet = wbook.add_sheet(rsheet.name)
    
    for i in range(rsheet.nrows):
        for j in range(rsheet.ncols):
            wsheet.write(i,j,rsheet.cell_value(i,j))
    
    
    wbook.save('out.xlsx')
    
    
  • 相关阅读:
    mysql-四舍五入
    数据库基础
    大白话五种IO模型
    Python程序中的协程操作-gevent模块
    Python程序中的协程操作-greenlet模块
    协程基础
    Python程序中的线程操作-concurrent模块
    Python程序中的线程操作-线程队列
    Python程序中的线程操作-锁
    Python程序中的线程操作-守护线程
  • 原文地址:https://www.cnblogs.com/Richardo-M-Q/p/13339161.html
Copyright © 2011-2022 走看看