# -*- coding: utf-8 -*-
#xlrd和xlwt只支持xls文件读写,openpyxl只支持xlsx文件的读写操作
import xlrd
import xlwt
import win32com
from win32com.client import constants,Dispatch
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.writer.excel import ExcelWriter #--------------------------------使用win32com模块批量读入要处理的log文件--------------#
#注意表的计数是从1开始的和openpyxl类似
def win32ReadExcel(inputFile,num):
#存放数据字典
data_dic = {}
line = 0
for i in range(1,num+1):
readfile = inputFile + str(i) + '.xls'
print (readfile)
xlApp = win32com.client.Dispatch('Excel.Application')
wb = xlApp.Workbooks.Open(readfile) #取第一张表
ws = wb.Worksheets(1)
info = ws.UsedRange
rows = info.Rows.Count
print (rows)
# 建立存储数据的字典
# t = ws.Cells(1, 1).Value
# print (t)
for rx in range(0,rows-1):
temp_list = [] w1 = ws.Cells(rx+2, 1).Value
w2 = ws.Cells(rx+2, 2).Value
w3 = ws.Cells(rx+2, 3).Value
w4 = ws.Cells(rx+2, 4).Value
w5 = ws.Cells(rx+2, 5).Value
temp_list = [w1,w2,w3,w4,w5]
data_dic[line] = temp_list
line = line + 1
# for i in range(rows): # data_dic[line] = ws.Rows[i+1].Cells
# line = line + 1
print (data_dic)
return data_dic #------------------------------使用xlrd模块批量读入要处理的log文件--------------#
def readExcel(inputFile,num):
#存放数据字典
data_dic = {}
line = 0
for i in range(1,num+1):
readfile = inputFile + str(i) + '.xls'
print (readfile)
wb = xlrd.open_workbook(readfile) #取第一张表
ws = wb.sheets()[0]
rows = ws.nrows
print (rows)
# 建立存储数据的字典
for rx in range(0,rows-1):
# print (rx)
data_dic[line] = ws.row_values(rx+1)
line = line + 1
return data_dic #-------------------------生成的xls格式的错误日志信息---------------------#
#注意xls格式的表示从0开始计数的
def exportXlsExcel(outputFile,data_log):
file = xlwt.Workbook(encoding='utf-8',style_compression=0)
table = file.add_sheet('sheet1',cell_overwrite_ok=True)
table.write(0,0,'1')
table.write(0,1,'2')
table.write(0,2,'3')
table.write(0,3,'4')
table.write(0,4,'5')
#读取过滤后的日志信息并写入xls文件中
for i in range(0,len(data_log)):
for j in range(0,5):
table.write(i+1,j,data_log[i][j]) file.save(outputFile); #---------------------------------程序入口---------------------------------#
if __name__ == '__main__':
data_dic = {}
data_dic = win32ReadExcel('H:\Practice\j730_5_',3)
#data_log = dealLog(data_dic)
#exportXlsxExcel('bbbb3.xlsx',data_log)
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. 模拟用户登录含注册——python第8天

    print('欢迎登录尚雅梦想python学习系统'.center(30)) print('******' * 8) flag = True while flag: order = input(''' ...

  2. 使用systemctl管理服务

    系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...

  3. leetcode.排序.75颜色分类-Java

    1. 具体题目 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列.此题中,我们使用整数 0. 1 和 2 分别表示红色. ...

  4. Linux上用sublime编辑Python时候出现"SyntaxError: Non-ASCII character ‘\xe5′ in file"的问题

    Reopen with Encoding UTF-8也没用,那只是在编辑界面里面显示出中文而已,编译的时候还是不能识别中文. 所以,还是按网上说的吧,在每一个有中文的文件第一行加上# -*- codi ...

  5. AJAX 步骤分析

    HTML 步骤: 1. 创建异步对象 2. 设置请求行 3. 设置请求头(get请求可以省略) 4. 注册状态改变事件 4.1. 判断状态&请求是否成功并使用数据   5. 发送请求   PH ...

  6. for循环(C语言型)举例

  7. 分支结构case 语句举例

  8. 一、Nuget管理

    一.注册账号 http://www.nuget.org 可用微软账户登录注册 手机端授权 二.打包Nuget 1.新建一个.NET Standard 的类库项目(取名类库) 更改方法 2.选择项目属性 ...

  9. python模块打补丁

    先自定义两个模块,然后,我们调用模块时,用打补丁方式,改写mod_1.py模块.为mod_2.py内容:其实这就相当于,在不改动mod_1.py模块的前提下,打上补丁. 写这个主要是gevent协程的 ...

  10. Oracle Linux下使用sqlplus的edit命令

    1.使当前会话生效 define_editor=vi SQL> select * from dual; D - X SQL> edit Wrote file afiedt.buf 21 1 ...