Python中xlrd、xlwt、win32com模块对xls文件的读写操作
# -*- 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文件的读写操作的更多相关文章
- Python中xlrd和xlwt模块使用方法 (python对excel文件的操作)
本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 安装xlrd和xlwt模块 xlrd和xlwt模块不是 ...
- python中常⽤的excel模块库
python中常用的excel模块库&安装方法 openpyxl openpyxl是⼀个Python库,用于读取/写⼊Excel 2010 xlsx / xlsm / xltx / xltm⽂ ...
- Python中os和shutil模块实用方法集…
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- Python中os和shutil模块实用方法集锦
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- python中根据字符串导入模块module
python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...
- python中的第三方日志模块logging
基本上每个系统都有自己的日志系统,可以使自己写的,也可以是第三方的.下面来简单介绍一下python中第三方的日志模块,入手还是比较简单的,但是也很容易给自己埋雷. 下面是我参考的资料链接 入手demo ...
- python中time、datetime模块的使用
目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...
- python中的logging日志模块
日志是程序不可或缺的一部分.它可以记录程序的运行情况,帮助我们更便捷地发现问题,而python中的logging日志模块给我们提供了这个机会. logging给我们提供了五种函数用来输出日志:debu ...
- Python中好用的模块们
目录 Python中好用的模块们 datetime模块 subprocess模块 matplotlib折线图 importlib模块 Python中好用的模块们 datetime模块 相信我们都使 ...
随机推荐
- asciinema.org -Record Your Terminal Share it with no fuss
紀錄 Terminal 下指令的過程 http://asciinema.org/
- IE的debug工具对程序进行debug跟踪JS代码
2015/8/31 (其他的:显示zjfy_app_sys_ip.html,只需关闭启用保护模式) 显示ie的debug,F12-->Ctrl + P 扩展:第一步,在程序中设置断点,如图所示左 ...
- k8s的快速使用手册
一.快速搭建文档 一.初始化kubernete kubeadm init --kubernetes-version=v1. --apiserver-advertise-address=192.168. ...
- react学习笔记_02-元素渲染
const element = <h1>Hello, world</h1>; 上面的内容代表react中的一个元素,元素是构成 React 应用的最小砖块. 与浏览器的 DOM ...
- 如何优雅地在React中处理事件响应&&React绑定onClick为什么要用箭头函数?
React绑定onClick为什么要用箭头函数? https://segmentfault.com/q/1010000010918131 如何优雅地在React中处理事件响应 https://segm ...
- IMU预积分
https://www.sohu.com/a/242760307_715754 http://www.sohu.com/a/243155537_715754 https://www.sohu.com/ ...
- 三、SpringBoot项目探究
1.pom文件 父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 77th ploty 制图神器
https://blog.csdn.net/u012897374/article/details/77857980
- 21.与重入锁相关联的Condition
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; /** * ...
- oracle 中||
oracle里双竖线是字符串连接运算符!