• python第三方库-xlrd-xlwt的使用


    复制内容http://blog.csdn.net/wangkai_123456/article/details/50457284

    一、xlrd和xlwt的安装

            xlrd和xlwt是python的第三方库,所以是需要自己安装的,可以在python的官网https://pypi.python.org/pypi下载该模块来安装,也可以通过其他手段,比如easy_install或者pip,我在Win7环境下已经安装好pip,所以就在命令符窗口中用如下命令来安装xlrd和xlwt。

    pip install xlrd

    pip install xlwt

    二、xlrd使用介绍

        1、导入模块
            import xlrd
       2、打开Excel文件读取数据
            data = xlrd.open_workbook('excelFile.xls')
       3、使用技巧
            获取一个工作表
            table = data.sheets()[0]          #通过索引顺序获取
            table = data.sheet_by_index(0) #通过索引顺序获取

            table = data.sheet_by_name(u'Sheet1')#通过名称获取

            获取整行和整列的值(数组)  
            table.row_values(i)
            table.col_values(i)
     
            获取行数和列数  
            nrows = table.nrows 
            ncols = table.ncols
           
            循环行列表数据
            for i in range(nrows):
                   print table.row_values(i)
     
            单元格
            cell_A1 = table.cell(0,0).value
            cell_C4 = table.cell(2,3).value
     
            使用行列索引
            cell_A1 = table.row(0)[0].value
            cell_A2 = table.col(1)[0].value
     
            简单的写入
            row = 0
            col = 0
     
            # 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
            ctype = 1 value = '单元格的值'
            xf = 0 # 扩展的格式化
            table.put_cell(row, col, ctype, value, xf)
            table.cell(0,0)  #单元格的值'
            table.cell(0,0).value #单元格的值'

    # _*_ coding:utf-8 _*_

    #----------------------------------------------------------------------------
    # import modules
    #----------------------------------------------------------------------------
    import os
    import xlrd
    from datetime import date,datetime

    三、xlwt使用介绍

    Xlwt简介:
    功能:用于生成97/2000/XP/2003xls文件。
    Python版本:Python2.3 to 2.7
    当前版本:0.7.5
    平台:跨平台
            
    相关模块:
    csv             python标准模块。推荐。
    xlrd            读取 .xls, .xlsx 文件。
    xlutils       xlrd和xlwt的集合。
    openpyxl 读写 Excel 2007 xlsx/xlsm文件 。纯python,效率不高。
    Matplotlib      2D作图模块,适用于基于excel作图。
    Pywin32   python windows扩展 ,不跨平台,通过COM口连接excel。
    Pyxll           在excel中使用python替代vbs。http://www.pyxll.com/。类似模块有pyinex。Python For Excel http://www.opentradingsystem.com/PythonForExcel/main.html
    XlsxWriter      写xlsx文件。纯python。
     
    快速入门
    下面例子,创建一个名为mini.xls的文件,它有一个空sheet:'xlwt was here'。代码见mini.py。
    fromxlwt import *
    w =Workbook()
    ws = w.add_sheet('xlwtwas here')
    w.save('mini.xls') 

    Workbook类初始化时有encoding和style_compression参数。

    encoding,设置字符编码,一般要这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。默认是ascii。当然要记得在文件头部添加:

    # -*- coding: utf-8-*-

    style_compression 表示是否压缩,不常用。
    Workbook 还有一些属性:
    Owner 设置文档所有者。
    country_code: 国家码
    wnd_protect: 窗口保护
    obj_protect: 对象保护
    Protect: 保护
    backup_on_save: 保存时备份
    Hpos: 横坐标
    Vpos: 纵坐标
    Width: 宽度
    Height: 高度
    active_sheet: 活动sheet
    tab_width: tab宽度
    wnd_visible: 窗口是否可见
    wnd_mini: 窗口最小化
    hscroll_visible: 横向滚动条是否可见。
    vscroll_visible: 纵向滚动条是否可见。
    tabs_visible: tab是否可见。
    dates_1904: 是否使用1904日期系统
    use_cell_values: 单元格的值
    default_style: 默认样式
    colour_RGB: 颜色

    方法有:add_style,add_font,add_str,del_str,str_index,add_rt,rt_index,add_sheet,get_sheet,raise_bad_sheetname,convert_sheetindex,setup_xcall,add_sheet_reference。



    # _*_ coding:utf-8 _*_

    #----------------------------------------------------------------------------
    # import modules
    #----------------------------------------------------------------------------
    import os
    import xlwt


    def set_style(name, height, bold = False):
    style = xlwt.XFStyle() #初始化样式

    font = xlwt.Font() #为样式创建字体
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height

    style.font = font
    return style


    def write_excel():
    #创建工作簿
    workbook = xlwt.Workbook(encoding='utf-8')
    #创建sheet
    data_sheet = workbook.add_sheet('demo')
    row0 = [u'字段名称', u'大致时段', 'CRNTI', 'CELL-ID']
    row1 = [u'测试', '15:50:33-15:52:14', 22706, 4190202]

    #生成第一行和第二行
    for i in range(len(row0)):
    data_sheet.write(0, i, row0[i], set_style('Times New Roman', 220, True))
    data_sheet.write(1, i, row1[i], set_style('Times New Roman', 220, True))

    #保存文件
    workbook.save('demo.xls')


    if __name__ == '__main__':
    write_excel()
    print u'创建demo.xlsx文件成功'

  • 相关阅读:
    PowerShell里的数组
    题解 cf gym 103261 D FFT Algorithm
    欧拉筛求积性函数的一般形式
    题解 LOJ 6053
    题解 SP34112 UDIVSUM
    题解 luogu P5162 WD与积木
    Java 关闭流同步输入输出
    题解 luogu P6620 [省选联考 2020 A 卷] 组合数问题
    hdu 6588 题解
    题解 cf gym 102979 E Expected Distance
  • 原文地址:https://www.cnblogs.com/hm-hx-ldy/p/8511475.html
Copyright © 2020-2023  润新知