• Python中xlwt解析


    1、导入模块

    import xlwt

    2、构造excel表

    workbook = xlwt.Workbook()                      #返回一个工作簿对象

    3、构造sheet

    workbook.add_sheet('info',cell_overwrite_ok=True)   #添加name为info的sheet

    4、构造sheet的格式

    style=xlwt.XFStyle()

    1)  Formatting the Contents of a Cell

    fnt = xlwt.Font()

    fnt.name = u'微软雅黑'

    fnt.colour_index = 2           

    #0x01 # 字体颜色默认为黑色,此处设置字体颜色为白色, 颜色范围为:0x00-0xff,也可

    以纯数字表示。

    fnt.underline = True

    fnt.italic = True

    fnt.bold = True

    fnt.height = 160                #font size:8pt

    style.font = fnt

    2)  Setting the Background Color of a Cell

    pattern = xlwt.Pattern()

    pattern.pattern = xlwt.Pattern.SOLID_PATTERN

    # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12

    pattern.pattern_fore_colour = 5

    # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 =Light Gray, 23 = Dark Gray, the list goes on

    style.pattern = pattern                  # Add Pattern to Style

    3)  Adding Borders to a Cell

    border = xlwt.Borders()

    border.left =xlwt.Borders.THIN

    border.top =xlwt.Borders.THIN

    border.right =xlwt.Borders.THIN

    border.bottom =xlwt.Borders.THIN

    # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED,MEDIUM_DASH_DOTTED,THIN_DASH_DOT_DOTTED,MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.

    border.left_color = 0x40

    border.top_color = 0x40

    border.right_color = 0x40

    border.bottom_color = 0x40

    style.borders =border

    4)  Setting the Alignment for the Contents of a Cell

    alignment = xlwt.Alignment()

    style.alignment.horz = HORZ_CENTER      #水平居中 值为2

    style.alignment.vert = VERT_CENTER      #垂直居中 值为1

    style.alignment.wrap = 1                # 自动换行

    style.alignment = alignment

    #May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT,HORZ_FILLED,

    HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED

    sheet1.col(0).width = 256*20       

    #默认字体0的1/256为衡量单位。默认宽度为2960,既11个字符0的宽度。256为衡量单位,20表示20个字符宽度

    5)  Entering a Date into a Cell

    style.num_format_str = 'M/D/YY'

    # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0

    worksheet.write(0, 0, datetime.datetime.now(), style)

    时间类型的转换,把excel中时间转成python 时间(两种方式)

    xlrd.xldate_as_tuple(table.cell(2,2).value, 0)   #转化为元组形式

    xlrd.xldate.xldate_as_datetime(table.cell(2,2).value, 1)   #直接转化为datetime对象

    xlrd.xldate_as_tuple(d,0)         

    #d  是从excel中读取出来的浮点数

    #第二个参数:有两种取值,0是以1900-01-01为基准的日期,而1是1904-01-01为基准的日期该函数返回的是一个元组,他的值类似:(year, month, day, hour, minute, nearest_second)

    6)  Adding a Formula to a Cell

    worksheet.write(1, 0, xlwt.Formula('A1*B1'))

    # Should output "10" (A1[5] * A2[2])

    worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))

    # Should output "7" (A1[5] + A2[2])

    7)  Merging Columns and Rows

    worksheet.write_merge(0, 0, 0, 3, 'First Merge')

    # Merges row 0's columns 0 through 3.

    worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style)

    # Merges row 1 through 2's columns 0 through 3

    8)  Adding a Hyperlink to a Cell

    worksheet.write(0,0,xlwt.Formula('HYPERLINK("http://www.google.com";"Google")'))

    # Outputs the text "Google" linking to http://www.google.com

    5、写入数据到sheet

    sheet1.write(0,0,'ok')                  #在指定行列写入数据

    6、保存excel

    workbook.save('ok.xls')                 #对工作簿进行保存

  • 相关阅读:
    Linux 日志管理
    Linux 备份与恢复
    Linux 启动管理
    Linux 用户和用户组管理
    产生指定时间区间序列、按指定单位变化时间 python实现
    python上数据存储 .h5格式或者h5py
    数据预处理之独热编码(One-Hot Encoding)
    残差网络
    GBDT为什么不能并行,XGBoost却可以
    百融金服、趣店、中航信面试总结
  • 原文地址:https://www.cnblogs.com/windyrainy/p/10595103.html
Copyright © 2020-2023  润新知