Python:Excel文件的写入

系统 1713 0

需要用到的库:

  1. 操作xls格式的表格文件:

    读取: xlrd

    写入: xlwt

    修改(追加写入): xlutils 操作 Excel 文件的实用工具,如复制、分割、筛选等

  2. 操作xlsx格式的表格文件:

    读取/写入: openpyxl

新建,写入内容,保存。

            
              
                #coding=utf-8  
              
              
                import
              
               xlwt  

              
                import
              
               xlrd  

              
                try
              
              
                :
              
              
                # 创建excel文件  
              
              
    filename
              
                =
              
              xlwt
              
                .
              
              Workbook
              
                (
              
              
                )
              
              
                # 给工作表命名,test  
              
              
    sheet
              
                =
              
              filename
              
                .
              
              add_sheet
              
                (
              
              
                "test"
              
              
                )
              
              
                # 写入内容,第4行第3列写入‘张三丰’  
              
              
    hello
              
                =
              
              u
              
                '张三丰'
              
                
    sheet
              
                .
              
              write
              
                (
              
              
                3
              
              
                ,
              
              
                2
              
              
                ,
              
              hello
              
                )
              
              
                # 指定存储路径,如果当前路径存在同名文件,会覆盖掉同名文件  
              
              
    filename
              
                .
              
              save
              
                (
              
              
                "D:/test1.xls"
              
              
                )
              
              
                except
              
               Exception
              
                ,
              
              e
              
                :
              
              
                print
              
              
                (
              
              
                str
              
              
                (
              
              e
              
                )
              
              
                )
              
            
          

简单的读取

            
              
                #coding=utf-8  
              
              
                import
              
               xlwt  

              
                import
              
               xlrd  

              
                try
              
              
                :
              
              
                # 创建excel文件  
              
              
    filename
              
                =
              
              xlwt
              
                .
              
              Workbook
              
                (
              
              
                )
              
              
                # 给工作表命名,test  
              
              
    sheet
              
                =
              
              filename
              
                .
              
              add_sheet
              
                (
              
              
                "test"
              
              
                )
              
              
                # 写入内容,第4行第3列写入‘张三丰’  
              
              
    hello
              
                =
              
              u
              
                '张三丰'
              
                
    sheet
              
                .
              
              write
              
                (
              
              
                3
              
              
                ,
              
              
                2
              
              
                ,
              
              hello
              
                )
              
              
                # 指定存储路径,如果当前路径存在同名文件,会覆盖掉同名文件  
              
              
    filename
              
                .
              
              save
              
                (
              
              
                "D:/test1.xls"
              
              
                )
              
              
                except
              
               Exception
              
                ,
              
              e
              
                :
              
              
                print
              
              
                (
              
              
                str
              
              
                (
              
              e
              
                )
              
              
                )
              
              
                #     找到读取文件  
              
              
filename
              
                =
              
              
                'D:/test1.xls'
              
              
                # 打开excel文件  
              
              
date
              
                =
              
              xlrd
              
                .
              
              open_workbook
              
                (
              
              filename
              
                )
              
              
                # 根据工作表名称,找到指定工作表  by_index(0)找到第N个工作表  
              
              
sheet
              
                =
              
              date
              
                .
              
              sheet_by_name
              
                (
              
              
                'test'
              
              
                )
              
              
                # 读取第四行第三列内容,cell_value读取单元格内容,指定编码  
              
              
value
              
                =
              
              sheet
              
                .
              
              cell_value
              
                (
              
              
                3
              
              
                ,
              
              
                2
              
              
                )
              
              
                .
              
              encode
              
                (
              
              
                'utf-8'
              
              
                )
              
              
                print
              
              
                (
              
              value
              
                )
              
            
          

字典格式

            
              
                import
              
                xlwt


              
                file
              
              
                =
              
              xlwt
              
                .
              
              Workbook
              
                (
              
              encoding
              
                =
              
              
                "utf8"
              
              
                )
              
              
table
              
                =
              
              
                file
              
              
                .
              
              add_sheet
              
                (
              
              
                "data"
              
              
                )
              
              
                #字典数据
              
              
data 
              
                =
              
              
                {
              
              
                "1"
              
              
                :
              
              
                [
              
              
                "张三"
              
              
                ,
              
              
                150
              
              
                ,
              
              
                120
              
              
                ,
              
              
                100
              
              
                ]
              
              
                ,
              
              
                "2"
              
              
                :
              
              
                [
              
              
                "李四"
              
              
                ,
              
              
                90
              
              
                ,
              
              
                99
              
              
                ,
              
              
                95
              
              
                ]
              
              
                ,
              
              
                "3"
              
              
                :
              
              
                [
              
              
                "王五"
              
              
                ,
              
              
                60
              
              
                ,
              
              
                66
              
              
                ,
              
              
                68
              
              
                ]
              
              
                }
              
              

ldata
              
                =
              
              
                [
              
              
                ]
              
              
                # 对字典的遍历,其实是对‘键’的遍历
              
              
num
              
                =
              
              
                [
              
              a 
              
                for
              
                a 
              
                in
              
               data
              
                ]
              
              
                #for循环将data字典中的键和值分批的保存在ldata中
              
              
                for
              
               x 
              
                in
              
               num
              
                :
              
              
    t
              
                =
              
              
                [
              
              
                int
              
              
                (
              
              x
              
                )
              
              
                ]
              
              
                for
              
               a 
              
                in
              
               data
              
                [
              
              x
              
                ]
              
              
                :
              
              
        t
              
                .
              
              append
              
                (
              
              a
              
                )
              
              

    ldata
              
                .
              
              append
              
                (
              
              t
              
                )
              
              
                #将数据写入文件,i是enumerate()函数返回的序号数
              
              
                for
              
               i
              
                ,
              
              p 
              
                in
              
              
                enumerate
              
              
                (
              
              ldata
              
                )
              
              
                :
              
              
                for
              
               j 
              
                ,
              
              q 
              
                in
              
              
                enumerate
              
              
                (
              
              p
              
                )
              
              
                :
              
              
        table
              
                .
              
              write
              
                (
              
              i
              
                ,
              
              j
              
                ,
              
              q
              
                )
              
              
                file
              
              
                .
              
              save
              
                (
              
              
                "test.xls"
              
              
                )
              
            
          

Python:Excel文件的写入_第1张图片


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论