1. # -*- coding: utf-8 -*-
  2. #xlrd和xlwt只支持xls文件读写,openpyxl只支持xlsx文件的读写操作
  3. import xlrd
  4. import xlwt
  5. import win32com
  6. from win32com.client import constants,Dispatch
  7. from openpyxl import load_workbook
  8. from openpyxl import Workbook
  9. from openpyxl.writer.excel import ExcelWriter
  10. #--------------------------------使用win32com模块批量读入要处理的log文件--------------#
  11. #注意表的计数是从1开始的和openpyxl类似
  12. def win32ReadExcel(inputFile,num):
  13. #存放数据字典
  14. data_dic = {}
  15. line = 0
  16. for i in range(1,num+1):
  17. readfile = inputFile + str(i) + '.xls'
  18. print (readfile)
  19. xlApp = win32com.client.Dispatch('Excel.Application')
  20. wb = xlApp.Workbooks.Open(readfile)
  21. #取第一张表
  22. ws = wb.Worksheets(1)
  23. info = ws.UsedRange
  24. rows = info.Rows.Count
  25. print (rows)
  26. # 建立存储数据的字典
  27. # t = ws.Cells(1, 1).Value
  28. # print (t)
  29. for rx in range(0,rows-1):
  30. temp_list = []
  31. w1 = ws.Cells(rx+2, 1).Value
  32. w2 = ws.Cells(rx+2, 2).Value
  33. w3 = ws.Cells(rx+2, 3).Value
  34. w4 = ws.Cells(rx+2, 4).Value
  35. w5 = ws.Cells(rx+2, 5).Value
  36. temp_list = [w1,w2,w3,w4,w5]
  37. data_dic[line] = temp_list
  38. line = line + 1
  39. # for i in range(rows):
  40. # data_dic[line] = ws.Rows[i+1].Cells
  41. # line = line + 1
  42. print (data_dic)
  43. return data_dic
  44. #------------------------------使用xlrd模块批量读入要处理的log文件--------------#
  45. def readExcel(inputFile,num):
  46. #存放数据字典
  47. data_dic = {}
  48. line = 0
  49. for i in range(1,num+1):
  50. readfile = inputFile + str(i) + '.xls'
  51. print (readfile)
  52. wb = xlrd.open_workbook(readfile)
  53. #取第一张表
  54. ws = wb.sheets()[0]
  55. rows = ws.nrows
  56. print (rows)
  57. # 建立存储数据的字典
  58. for rx in range(0,rows-1):
  59. # print (rx)
  60. data_dic[line] = ws.row_values(rx+1)
  61. line = line + 1
  62. return data_dic
  63. #-------------------------生成的xls格式的错误日志信息---------------------#
  64. #注意xls格式的表示从0开始计数的
  65. def exportXlsExcel(outputFile,data_log):
  66. file = xlwt.Workbook(encoding='utf-8',style_compression=0)
  67. table = file.add_sheet('sheet1',cell_overwrite_ok=True)
  68. table.write(0,0,'1')
  69. table.write(0,1,'2')
  70. table.write(0,2,'3')
  71. table.write(0,3,'4')
  72. table.write(0,4,'5')
  73. #读取过滤后的日志信息并写入xls文件中
  74. for i in range(0,len(data_log)):
  75. for j in range(0,5):
  76. table.write(i+1,j,data_log[i][j])
  77. file.save(outputFile);
  78. #---------------------------------程序入口---------------------------------#
  79. if __name__ == '__main__':
  80. data_dic = {}
  81. data_dic = win32ReadExcel('H:\Practice\j730_5_',3)
  82. #data_log = dealLog(data_dic)
  83. #exportXlsxExcel('bbbb3.xlsx',data_log)
  84. exportXlsExcel('bbbb.xls',data_log)

from: https://blog.csdn.net/revitalizing/article/details/47423427

Python中xlrd、xlwt、win32com模块对xls文件的读写操作的更多相关文章

  1. Python中xlrd和xlwt模块使用方法 (python对excel文件的操作)

    本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 安装xlrd和xlwt模块 xlrd和xlwt模块不是 ...

  2. python中常⽤的excel模块库

    python中常用的excel模块库&安装方法 openpyxl openpyxl是⼀个Python库,用于读取/写⼊Excel 2010 xlsx / xlsm / xltx / xltm⽂ ...

  3. Python中os和shutil模块实用方法集…

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

  4. Python中os和shutil模块实用方法集锦

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

  5. python中根据字符串导入模块module

    python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...

  6. python中的第三方日志模块logging

    基本上每个系统都有自己的日志系统,可以使自己写的,也可以是第三方的.下面来简单介绍一下python中第三方的日志模块,入手还是比较简单的,但是也很容易给自己埋雷. 下面是我参考的资料链接 入手demo ...

  7. python中time、datetime模块的使用

    目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...

  8. python中的logging日志模块

    日志是程序不可或缺的一部分.它可以记录程序的运行情况,帮助我们更便捷地发现问题,而python中的logging日志模块给我们提供了这个机会. logging给我们提供了五种函数用来输出日志:debu ...

  9. Python中好用的模块们

    目录 Python中好用的模块们 datetime模块 subprocess模块 matplotlib折线图 importlib模块 Python中好用的模块们 datetime模块 ​ 相信我们都使 ...

随机推荐

  1. asciinema.org -Record Your Terminal Share it with no fuss

    紀錄 Terminal 下指令的過程 http://asciinema.org/

  2. IE的debug工具对程序进行debug跟踪JS代码

    2015/8/31 (其他的:显示zjfy_app_sys_ip.html,只需关闭启用保护模式) 显示ie的debug,F12-->Ctrl + P 扩展:第一步,在程序中设置断点,如图所示左 ...

  3. k8s的快速使用手册

    一.快速搭建文档 一.初始化kubernete kubeadm init --kubernetes-version=v1. --apiserver-advertise-address=192.168. ...

  4. react学习笔记_02-元素渲染

    const element = <h1>Hello, world</h1>; 上面的内容代表react中的一个元素,元素是构成 React 应用的最小砖块. 与浏览器的 DOM ...

  5. 如何优雅地在React中处理事件响应&&React绑定onClick为什么要用箭头函数?

    React绑定onClick为什么要用箭头函数? https://segmentfault.com/q/1010000010918131 如何优雅地在React中处理事件响应 https://segm ...

  6. IMU预积分

    https://www.sohu.com/a/242760307_715754 http://www.sohu.com/a/243155537_715754 https://www.sohu.com/ ...

  7. 三、SpringBoot项目探究

    1.pom文件 父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. 77th ploty 制图神器

    https://blog.csdn.net/u012897374/article/details/77857980

  9. 21.与重入锁相关联的Condition

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; /** * ...

  10. oracle 中||

    oracle里双竖线是字符串连接运算符!