精华内容
下载资源
问答
  • python 安装 xlwt 模块

    万次阅读 2018-11-07 14:46:10
    xlwt的简介 这是一个开发人员用来生成与微软Excel版本95到2003兼容的电子表格文件的...xlwt安装 xlwt xlrd 模块 分别在https://pypi.python.org/pypi/xlwt 和 https://pypi.python.org/pypi/xlrd windows: pip3 in...

    xlwt的简介
    这是一个开发人员用来生成与微软Excel版本95到2003兼容的电子表格文件的库。包本身是纯Python,在标准Python分布之外没有依赖于模块或包的依赖关系。
    xlwt的安装
    xlwt xlrd 模块

    分别在https://pypi.python.org/pypi/xlwt
    https://pypi.python.org/pypi/xlrd
    windows:

    pip3 install xlwt
    pip3 install xlwt


    Collecting xlwt
    Downloading https://files.pythonhosted.org/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl (99kB)
    100% |████████████████████████████████| 102kB 42kB/s
    Installing collected packages: xlwt
    Successfully installed xlwt-1.3.0
    You are using pip version 10.0.1, however version 18.1 is available.
    You should consider upgrading via the ‘python -m pip install --upgrade pip’ command.


    linux:



    xlwt的使用方法

    import xlwt
    from datetime import datetime
     
    style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
        num_format_str='#,##0.00')
    style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
     
    wb = xlwt.Workbook()
    ws = wb.add_sheet('A Test Sheet')
     
    ws.write(0, 0, 1234.56, style0)
    ws.write(1, 0, datetime.now(), style1)
    ws.write(2, 0, 1)
    ws.write(2, 1, 1)
    ws.write(2, 2, xlwt.Formula("A3+B3"))
     
    wb.save('example.xls')
    
    
    from xlwt import *    #需要xlwt库的支持  或者#import xlwt
     
    def write_to_excle(data):
        file = Workbook(encoding = 'utf-8')  #指定file以utf-8的格式打开
        table = file.add_sheet('data')       #指定打开的文件名,sheet名
        ldata = []
        num = [a for a in data]   #for循环指定取出key值存入num中
        num.sort()                #字典数据取出后无需,需要先排序
        
        for x in num:             #for循环将data字典中的键和值分批的保存在ldata中
            t = [int(x)]
            for a in data[x]:
                t.append(a)
            ldata.append(t)
         
        for i,p in enumerate(ldata): #将数据写入文件,i是enumerate()函数返回的序号数
            for j,q in enumerate(p):
                print(i,j,q)
                table.write(i,j,q)
        file.save('data.xlsx')
            
    row01=["第一行",11,12]
    row02=["第二行",21,22]
    row03=["第三行",31,32]
     
    data = {
            "1": row01,
            "2":row02,
            "3":row03
            }
    write_to_excle(data)
    
    展开全文
  • 主要介绍了Python使用xlwt模块操作Excel的方法,结合实例形式分析了Python安装xlwt模块及使用xlwt模块针对Excel文件的创建、设置、保存等常用操作技巧,需要的朋友可以参考下
  • centos6.5下怎么安装pythonxlwt模块。我得python是linux自带的。版本:Python 2.6.6 怎么安装这个模块xlwt
  • pythonxlwt模块

    2018-09-20 14:56:00
    python官网下载https://pypi.python.org/pypi/xlwt模块安装。 ♦或者在cmd窗口pip installxlrd 二、使用 1.导入模块 import xlwt 2.例子 #!/usr/bin/python #coding=utf-8 # =====================...

    一、安装

    ♦ python官网下载https://pypi.python.org/pypi/xlwt模块安装。

    ♦或者在cmd窗口  pip install  xlrd

    二、使用

    1.导入模块

    import xlwt

    2.例子

    #!/usr/bin/python
    #coding=utf-8
    # ==============================================================================
    #
    #    Filename: style.py
    #  Description: style
    #    Created: Thu Apr 27 15:07:53 CST 2017
    #     Author: Yur
    #
    # ==============================================================================
    import xlwt
    workbook = xlwt.Workbook(encoding = 'ascii')
    worksheet = workbook.add_sheet('My Worksheet')
    style = xlwt.XFStyle() # 初始化样式
    font = xlwt.Font() # 为样式创建字体
    font.name = 'Times New Roman'
    font.bold = True # 黑体
    font.underline = True # 下划线
    font.italic = True # 斜体字
    style.font = font # 设定样式
    worksheet.write(0, 0, 'Unformatted value') # 不带样式的写入
    worksheet.write(1, 0, 'Formatted value', style) # 带样式的写入
    workbook.save('formatting.xls') # 保存文件

     

    3.设置单元格宽度

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0,'My Cell Contents')
    # 设置单元格宽度
    worksheet.col(0).width = 3333
    workbook.save('cell_width.xls')

     4.输入一个日期到单元格:

    import xlwt
    import datetime
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    style = xlwt.XFStyle()
    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)
    workbook.save('Excel_Workbook.xls')

    5. 添加一个公式:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0, 5) # Outputs 5
    worksheet.write(0, 1, 2) # Outputs 2
    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])
    workbook.save('Excel_Workbook.xls')

     

    6. 添加一个超链接:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) # Outputs the text "Google" linking to http://www.google.com
    workbook.save('Excel_Workbook.xls')

    7.合并列和行:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.
    font = xlwt.Font() # Create Font
    font.bold = True # Set font to Bold
    style = xlwt.XFStyle() # Create Style
    style.font = font # Add Bold Font to Style
    worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.
    workbook.save('Excel_Workbook.xls')

     

    8.设置单元格内容的对其方式:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    alignment = xlwt.Alignment() # Create Alignment
    alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
    alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
    style = xlwt.XFStyle() # Create Style
    style.alignment = alignment # Add Alignment to Style
    worksheet.write(0, 0, 'Cell Contents', style)
    workbook.save('Excel_Workbook.xls')

     

    9.为单元格议添加边框:

    # Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.
    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    borders = xlwt.Borders() # Create Borders
    borders.left = xlwt.Borders.DASHED
      DASHED虚线
      NO_LINE没有
      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.
    borders.right = xlwt.Borders.DASHED
    borders.top = xlwt.Borders.DASHED
    borders.bottom = xlwt.Borders.DASHED
    borders.left_colour = 0x40
    borders.right_colour = 0x40
    borders.top_colour = 0x40
    borders.bottom_colour = 0x40
    style = xlwt.XFStyle() # Create Style
    style.borders = borders # Add Borders to Style
    worksheet.write(0, 0, 'Cell Contents', style)
    workbook.save('Excel_Workbook.xls')

     


    10.为单元格设置背景色:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    pattern = xlwt.Pattern() # Create the 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 = xlwt.XFStyle() # Create the Pattern
    style.pattern = pattern # Add Pattern to Style
    worksheet.write(0, 0, 'Cell Contents', style)
    workbook.save('Excel_Workbook.xls')

     

    转载于:https://www.cnblogs.com/gexbooks/p/9680974.html

    展开全文
  • python使用xlwt模块操作Excel

    万次阅读 多人点赞 2019-05-05 14:58:49
    模块安装很简单$ pip install xlwt先来个简单的例子:#!/usr/bin/python #coding=utf-8 # ============================================================================== # # Filename: demo.py # Descriptio

    部分摘自官网文档.

    该模块安装很简单

    $ pip install xlwt
    

    先来个简单的例子:

    #!/usr/bin/python
    #coding=utf-8
    # ==============================================================================
    #
    #       Filename:  demo.py
    #    Description:  excel operat
    #        Created:  Tue Apr 25 17:10:33 CST 2017
    #         Author:  Yur
    #
    # ==============================================================================
    
    import xlwt
    # 创建一个workbook 设置编码
    workbook = xlwt.Workbook(encoding = 'utf-8')
    # 创建一个worksheet
    worksheet = workbook.add_sheet('My Worksheet')
    
    # 写入excel
    # 参数对应 行, 列, 值
    worksheet.write(1,0, label = 'this is test')
    
    # 保存
    workbook.save('Excel_test.xls')
    

    运行后 会在当前目录生成一个Excel_test.xls

    官方例子:

    运行这个例子的时候 报错

    自己又写了一个:

    #!/usr/bin/python
    #coding=utf-8
    # ==============================================================================
    #
    #       Filename:  style.py
    #    Description:  style
    #        Created:  Thu Apr 27 15:07:53 CST 2017
    #         Author:  Yur
    #
    # ==============================================================================
    
    
    import xlwt
    
    workbook = xlwt.Workbook(encoding = 'ascii')
    worksheet = workbook.add_sheet('My Worksheet')
    style = xlwt.XFStyle() # 初始化样式
    font = xlwt.Font() # 为样式创建字体
    font.name = 'Times New Roman' 
    font.bold = True # 黑体
    font.underline = True # 下划线
    font.italic = True # 斜体字
    style.font = font # 设定样式
    worksheet.write(0, 0, 'Unformatted value') # 不带样式的写入
    
    worksheet.write(1, 0, 'Formatted value', style) # 带样式的写入
    
    workbook.save('formatting.xls') # 保存文件
    

    效果:

    设置单元格宽度:

    import xlwt
    
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0,'My Cell Contents')
    
    # 设置单元格宽度
    worksheet.col(0).width = 3333
    workbook.save('cell_width.xls')
    

    输入一个日期到单元格:

    import xlwt
    import datetime
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    style = xlwt.XFStyle()
    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)
    workbook.save('Excel_Workbook.xls')
    

    向单元格添加一个公式:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0, 5) # Outputs 5
    worksheet.write(0, 1, 2) # Outputs 2
    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])
    workbook.save('Excel_Workbook.xls')
    

    向单元格添加一个超链接:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) # Outputs the text "Google" linking to http://www.google.com
    workbook.save('Excel_Workbook.xls')
    

    合并列和行:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.
    font = xlwt.Font() # Create Font
    font.bold = True # Set font to Bold
    style = xlwt.XFStyle() # Create Style
    style.font = font # Add Bold Font to Style
    worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.
    workbook.save('Excel_Workbook.xls')
    

    设置单元格内容的对其方式:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    alignment = xlwt.Alignment() # Create Alignment
    alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
    alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
    style = xlwt.XFStyle() # Create Style
    style.alignment = alignment # Add Alignment to Style
    worksheet.write(0, 0, 'Cell Contents', style)
    workbook.save('Excel_Workbook.xls')
    

    为单元格议添加边框:

    # Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.
    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    borders = xlwt.Borders() # Create Borders
    borders.left = xlwt.Borders.DASHED 
        DASHED虚线
        NO_LINE没有
        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.
    borders.right = xlwt.Borders.DASHED
    borders.top = xlwt.Borders.DASHED
    borders.bottom = xlwt.Borders.DASHED
    borders.left_colour = 0x40
    borders.right_colour = 0x40
    borders.top_colour = 0x40
    borders.bottom_colour = 0x40
    style = xlwt.XFStyle() # Create Style
    style.borders = borders # Add Borders to Style
    worksheet.write(0, 0, 'Cell Contents', style)
    workbook.save('Excel_Workbook.xls')
    

    为单元格设置背景色:

    import xlwt
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    pattern = xlwt.Pattern() # Create the 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 = xlwt.XFStyle() # Create the Pattern
    style.pattern = pattern # Add Pattern to Style
    worksheet.write(0, 0, 'Cell Contents', style)
    workbook.save('Excel_Workbook.xls')
    
    展开全文
  • 今天给大家分享的是用Pythonxlwt 模块写入 Excel,下面是相关实现过程。 1、xlrd 安装 cmd 中输入pip install xlwt 即可安装 xlwt 模块 2、创建工作簿并添加工作表 import xlwt# 创建工作簿wb = xlwt.Workbook()#...

    在这里插入图片描述

    今天给大家分享的是用Python 的xlwt 模块写入 Excel,下面是相关实现过程。

    1、xlrd 安装
    cmd 中输入pip install xlwt 即可安装 xlwt 模块

    2、创建工作簿并添加工作表
    import xlwt# 创建工作簿wb = xlwt.Workbook()# 添加工作表ws = wb.add_sheet(“绩点”)

    3、填充数据
    ws.write_merge(5, 6, 4, 6, “绩点”, titlestyle) # (x1,y1,x2,y2,内容,样式)# (x1,y1)到(x2,y2)之间合并单元格,并以相应样式填写内容
    4、写入数据
    data = ((“学号”, “第一学期”, “第二学期”),(“201911040693”, 2.971, 2.939),(“201911040694”, 2.035, 2.487),(“201911040695”, 2.709, 2.099),(“201911040696”, 3.374, 3.388),(“201911040697”, 2.646, 2.415))for i, item in enumerate(data):for j, val in enumerate(item):ws.write(i + 7, j + 4, val, dataStyle) # (行,列,内容,样式)# 填写数据
    5、插入图片
    ws.insert_bitmap(“123.bmp”, 20, 21)
    6、保存文件
    wb.save(“前两学期绩点统计.xls”)
    以上就是关于用Python 的xlwt 模块写入 Excel的过程了,大家可以尝试操作一下。
    文章部分内容源于网络,联系侵删*

    展开全文
  • python安装xlwt、xlrd、django模块

    千次阅读 2020-04-14 13:45:20
    xlwd、xlrd模块用于python语言中对表格进行读写的模块。 看了很多方法都是去官网下载官网对应的安装包 https://pypi.python.org/pypi/xlwt https://pypi.python.org/pypi/xlrt cmd进入对应目录 python setup.py ...
  • 本文实例讲述了Python使用xlwt模块操作Excel的方法。分享给大家供大家参考,具体如下:部分摘自官网文档.该模块安装很简单$ pip install xlwt先来个简单的例子:#!/usr/bin/python#coding=utf-8# ===================...
  • 安装:pip install xlwt 文章目录一、示例 1二、示例 2三、设置单元格宽度四、输入一个日期到单元格:五、向单元格添加一个公式:六、向单元格添加一个超链接:七、合并列和行:八、设置单元格内容的对其方式:九、为...
  • xlwt模块可以在命令行中使用: pip install xlwt 来安装 安装完之后简易使用步骤 import xlwt #新建excel文件 file=xlwt.Workbook() #新建一张表(sheet) table=file.add_sheet('sheet name') #写入数据table....
  • Python笔记 之 xlwt模块

    2020-11-16 11:07:37
    使用pip安装xlwt模块 $ pip install xlwt 简单的官方例子 import xlwt from datetime import datetime style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00...
  • 安装:pip3 install xlwt 导入:import xlwt xlrd 模块方法 写入案例 import xlwt # 创建对象,设置编码 workbook = xlwt.Workbook(encoding=’utf-8′) # 新建sheet表 worksheet = workbook.add_sheet(sheet_...
  • 最近在工作中需要对EXCEL表进行操作,故而对xlrd和xlwt模块进行了一番学习,功能很强大,我就常用的一些基本功能做点总结吧。 模块的安装: xlrd和xlwt模块不是自带模块。需要进行安装安装方法不多说,有很多种...
  • 1. 安装模块 #pip install xlwt 2. 简单例子打印excel import xlwt # 创建一个workbook 设置编码 workbook = xlwt.Workbook(encoding = 'utf-8') # 创建一个worksheet worksheet = workbook.add_sheet...
  • Python模块xlwt对excel进行写入操作

    千次阅读 2019-04-19 19:11:00
    python常用模块目录 1、安装 $ pip install xlwt 2、创建表格和工作表单写入内容 例子: import xlwt # 创建一个workbook 设置编码 workbook = xlwt.Workbook(encoding = 'utf-8') #print(workbook) %结果...
  • 本文主要介绍可操作excel...安装xlrd和xlwt模块 xlrd和xlwt模块不是自带的,需要自行安装。模块安装建议使用pip自动安装安装方法参考<Python自动安装第三方模块> xlrd模块使用 测试使用excel文档名称为St...
  • 文章目录一、介绍二、安装方法三、 excel表格描述四、xlrd读取excel文件4.1 获取WorkBook对象4.1 xlrd操作sheet对象4.2 xlrd操作cell对象五、xlwt写入...xlwt模块:可写入.xls表格(不可写入.xlsx文件!!!) 二、安
  • 安装两个模块xlwt用来写excel,xlrd用来读excel 直接pip install xxx即可 以下简单示例: 读: wb = xlrd.open_workbook(file_name) #创建xlrd对象 sh = wb.sheet_by_index(0) #根据下标读取第一个sheet ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 465
精华内容 186
关键字:

python安装xlwt模块

python 订阅