Python笔记:使用pywin32处理excel文件
因为前端同事须要批量的对excel文件做特殊处理,删除指定行,故写了此脚本。同一时候配合config.ini方便不熟悉py的同事使用
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- #######################################################
- # 用于批量删除excel的指定行 #
- # 适用于全部office。前提须要安装pywin32和office软件 #
- #######################################################
- import os
- import sys
- import time
- import glob
- import shutil
- import string
- import os.path
- import traceback
- import ConfigParser
- import win32com.client
- SPATH = "" #需处理的excel文件文件夹
- DPATH = "" #处理后的excel存放文件夹
- SKIP_FILE_LIST = [] #须要跳过的文件列表
- MAX_SHEET_INDEX = 1 #每一个excel文件的前几个表须要处理
- DELETE_ROW_LIST = [] #须要删除的行号
- def dealPath(pathname=''):
- '''deal with windows file path'''
- if pathname:
- pathname = pathname.strip()
- if pathname:
- pathname = r'%s'%pathname
- pathname = string.replace(pathname, r'/', '\\')
- pathname = os.path.abspath(pathname)
- if pathname.find(":\\") == -1:
- pathname = os.path.join(os.getcwd(), pathname)
- return pathname
- class EasyExcel(object):
- '''class of easy to deal with excel'''
- def __init__(self):
- '''initial excel application'''
- self.m_filename = ''
- self.m_exists = False
- self.m_excel = win32com.client.DispatchEx('Excel.Application') #也能够用Dispatch,前者开启新进程,后者会复用进程中的excel进程
- self.m_excel.DisplayAlerts = False #覆盖同名文件时不弹出确认框
- def open(self, filename=''):
- '''open excel file'''
- if getattr(self, 'm_book', False):
- self.m_book.Close()
- self.m_filename = dealPath(filename) or ''
- self.m_exists = os.path.isfile(self.m_filename)
- if not self.m_filename or not self.m_exists:
- self.m_book = self.m_excel.Workbooks.Add()
- else:
- self.m_book = self.m_excel.Workbooks.Open(self.m_filename)
- def reset(self):
- '''reset'''
- self.m_excel = None
- self.m_book = None
- self.m_filename = ''
- def save(self, newfile=''):
- '''save the excel content'''
- assert type(newfile) is str, 'filename must be type string'
- newfile = dealPath(newfile) or self.m_filename
- if not newfile or (self.m_exists and newfile == self.m_filename):
- self.m_book.Save()
- return
- pathname = os.path.dirname(newfile)
- if not os.path.isdir(pathname):
- os.makedirs(pathname)
- self.m_filename = newfile
- self.m_book.SaveAs(newfile)
- def close(self):
- '''close the application'''
- self.m_book.Close(SaveChanges=1)
- self.m_excel.Quit()
- time.sleep(2)
- self.reset()
- def addSheet(self, sheetname=None):
- '''add new sheet, the name of sheet can be modify,but the workbook can't '''
- sht = self.m_book.Worksheets.Add()
- sht.Name = sheetname if sheetname else sht.Name
- return sht
- def getSheet(self, sheet=1):
- '''get the sheet object by the sheet index'''
- assert sheet > 0, 'the sheet index must bigger then 0'
- return self.m_book.Worksheets(sheet)
- def getSheetByName(self, name):
- '''get the sheet object by the sheet name'''
- for i in xrange(1, self.getSheetCount()+1):
- sheet = self.getSheet(i)
- if name == sheet.Name:
- return sheet
- return None
- def getCell(self, sheet=1, row=1, col=1):
- '''get the cell object'''
- assert row>0 and col>0, 'the row and column index must bigger then 0'
- return self.getSheet(sheet).Cells(row, col)
- def getRow(self, sheet=1, row=1):
- '''get the row object'''
- assert row>0, 'the row index must bigger then 0'
- return self.getSheet(sheet).Rows(row)
- def getCol(self, sheet, col):
- '''get the column object'''
- assert col>0, 'the column index must bigger then 0'
- return self.getSheet(sheet).Columns(col)
- def getRange(self, sheet, row1, col1, row2, col2):
- '''get the range object'''
- sht = self.getSheet(sheet)
- return sht.Range(self.getCell(sheet, row1, col1), self.getCell(sheet, row2, col2))
- def getCellValue(self, sheet, row, col):
- '''Get value of one cell'''
- return self.getCell(sheet,row, col).Value
- def setCellValue(self, sheet, row, col, value):
- '''set value of one cell'''
- self.getCell(sheet, row, col).Value = value
- def getRowValue(self, sheet, row):
- '''get the row values'''
- return self.getRow(sheet, row).Value
- def setRowValue(self, sheet, row, values):
- '''set the row values'''
- self.getRow(sheet, row).Value = values
- def getColValue(self, sheet, col):
- '''get the row values'''
- return self.getCol(sheet, col).Value
- def setColValue(self, sheet, col, values):
- '''set the row values'''
- self.getCol(sheet, col).Value = values
- def getRangeValue(self, sheet, row1, col1, row2, col2):
- '''return a tuples of tuple)'''
- return self.getRange(sheet, row1, col1, row2, col2).Value
- def setRangeValue(self, sheet, row1, col1, data):
- '''set the range values'''
- row2 = row1 + len(data) - 1
- col2 = col1 + len(data[0]) - 1
- range = self.getRange(sheet, row1, col1, row2, col2)
- range.Clear()
- range.Value = data
- def getSheetCount(self):
- '''get the number of sheet'''
- return self.m_book.Worksheets.Count
- def getMaxRow(self, sheet):
- '''get the max row number, not the count of used row number'''
- return self.getSheet(sheet).Rows.Count
- def getMaxCol(self, sheet):
- '''get the max col number, not the count of used col number'''
- return self.getSheet(sheet).Columns.Count
- def clearCell(self, sheet, row, col):
- '''clear the content of the cell'''
- self.getCell(sheet,row,col).Clear()
- def deleteCell(self, sheet, row, col):
- '''delete the cell'''
- self.getCell(sheet, row, col).Delete()
- def clearRow(self, sheet, row):
- '''clear the content of the row'''
- self.getRow(sheet, row).Clear()
- def deleteRow(self, sheet, row):
- '''delete the row'''
- self.getRow(sheet, row).Delete()
- def clearCol(self, sheet, col):
- '''clear the col'''
- self.getCol(sheet, col).Clear()
- def deleteCol(self, sheet, col):
- '''delete the col'''
- self.getCol(sheet, col).Delete()
- def clearSheet(self, sheet):
- '''clear the hole sheet'''
- self.getSheet(sheet).Clear()
- def deleteSheet(self, sheet):
- '''delete the hole sheet'''
- self.getSheet(sheet).Delete()
- def deleteRows(self, sheet, fromRow, count=1):
- '''delete count rows of the sheet'''
- maxRow = self.getMaxRow(sheet)
- maxCol = self.getMaxCol(sheet)
- endRow = fromRow+count-1
- if fromRow > maxRow or endRow < 1:
- return
- self.getRange(sheet, fromRow, 1, endRow, maxCol).Delete()
- def deleteCols(self, sheet, fromCol, count=1):
- '''delete count cols of the sheet'''
- maxRow = self.getMaxRow(sheet)
- maxCol = self.getMaxCol(sheet)
- endCol = fromCol + count - 1
- if fromCol > maxCol or endCol < 1:
- return
- self.getRange(sheet, 1, fromCol, maxRow, endCol).Delete()
- def echo(msg):
- '''echo message'''
- print msg
- def dealSingle(excel, sfile, dfile):
- '''deal with single excel file'''
- echo("deal with %s"%sfile)
- basefile = os.path.basename(sfile)
- excel.open(sfile)
- sheetcount = excel.getSheetCount()
- if not (basefile in SKIP_FILE_LIST or file in SKIP_FILE_LIST):
- for sheet in range(1, sheetcount+1):
- if sheet > MAX_SHEET_INDEX:
- continue
- reduce = 0
- for row in DELETE_ROW_LIST:
- excel.deleteRow(sheet, row-reduce)
- reduce += 1
- #excel.deleteRows(sheet, 2, 2)
- excel.save(dfile)
- def dealExcel(spath, dpath):
- '''deal with excel files'''
- start = time.time()
- #check source path exists or not
- spath = dealPath(spath)
- if not os.path.isdir(spath):
- echo("No this directory :%s"%spath)
- return
- #check destination path exists or not
- dpath = dealPath(dpath)
- if not os.path.isdir(dpath):
- os.makedirs(dpath)
- shutil.rmtree(dpath)
- #list the excel file
- filelist = glob.glob(os.path.join(spath, '*.xlsx'))
- if not filelist:
- echo('The path of %s has no excel file'%spath)
- return
- #deal with excel file
- excel = EasyExcel()
- for file in filelist:
- basefile = os.path.basename(file)
- destfile = os.path.join(dpath, basefile)
- dealSingle(excel, file, destfile)
- echo('Use time:%s'%(time.time()-start))
- excel.close()
- def loadConfig(configfile='./config.ini'):
- '''parse config file'''
- global SPATH
- global DPATH
- global SKIP_FILE_LIST
- global MAX_SHEET_INDEX
- global DELETE_ROW_LIST
- file = dealPath(configfile)
- if not os.path.isfile(file):
- echo('Can not find the config.ini')
- return False
- parser = ConfigParser.ConfigParser()
- parser.read(file)
- SPATH = parser.get('pathconfig', 'spath').strip()
- DPATH = parser.get('pathconfig', 'dpath').strip()
- filelist = parser.get('otherconfig', 'filelist').strip()
- index = parser.get('otherconfig', 'maxindex').strip()
- rowlist = parser.get('otherconfig', 'deleterows').strip()
- if filelist:
- SKIP_FILE_LIST = filelist.split(";")
- if rowlist:
- DELETE_ROW_LIST = map(int, rowlist.split(";"))
- MAX_SHEET_INDEX = int(index) if index else MAX_SHEET_INDEX
- def main():
- '''main function'''
- loadConfig()
- if SPATH and DPATH and MAX_SHEET_INDEX:
- dealExcel(SPATH, DPATH)
- raw_input("Please press any key to exit!")
- if __name__=="__main__":
- main()
config.ini文件例如以下:
- [pathconfig]
- #;spath表示须要处理的excel文件文件夹
- spath=./tests
- #;dpath表示处理后的excel文件文件夹
- dpath=./dest
- [otherconfig]
- #;filelist表示不须要做特殊处理的excel文件列表,以英文分号分隔
- filelist=
- #;maxindex表示须要处理每一个excel文件的前几张表
- maxindex=1
- #;deleterows表示须要删除的阿拉伯数字行号,用英文分号分隔
- deleterows=2;3
Python笔记:使用pywin32处理excel文件的更多相关文章
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- 用Python的pandas框架操作Excel文件中的数据教程
用Python的pandas框架操作Excel文件中的数据教程 本文的目的,是向您展示如何使用pandas 来执行一些常见的Excel任务.有些例子比较琐碎,但我觉得展示这些简单的东西与那些你可以在其 ...
- 使用pywin32处理excel文件
#!/usr/bin/env python #-*- coding:utf-8 -*- ####################################################### ...
- Python小实验——读&写Excel文件内容
安装xlrd模块和xlwt模块 读取Excel文件了内容需要额外的模块-- \(xlrd\),在官网上可以找到下载:https://pypi.python.org/pypi/xlrd#download ...
- python接口自动化21-下载excel文件(Content-Type:octets/stream)
前言 Content-Type类型为octets/stream,这种一般是文件类型了,比如有时候需要导出excel数据,下载excel这种场景如何用python来实现呢? 抓下载接口 1.下载的场景如 ...
- 基于Python的接口自动化-读写excel文件
引言 使用python进行接口测试时常常需要接口用例测试数据.断言接口功能.验证接口响应状态等,如果大量的接口测试用例脚本都将接口测试用例数据写在脚本文件中,这样写出来整个接口测试用例脚本代码将看起来 ...
- 记录python接口自动化测试--把操作excel文件的方法封装起来(第五目)
前面补充了如何来操作excel文件,这次把如何获取excel文件的sheet对象.行数.单元格数据的方法进行封装,方便后面调用 handle_excel.py# coding:utf-8 import ...
- Python使用xlwt模块 操作Excel文件
导出Excel文件 1. 使用xlwt模块 import xlwt import xlwt # 导入xlwt # 新建一个excel文件 file = xlwt.Workbook() # ...
- Python自动化办公之操作Excel文件
模块导入 import openpyxl 读取Excel文件 打开Excel文件 workbook = openpyxl.load_workbook("test.xlsx") 输出 ...
随机推荐
- 显示推送数据到mq成功,但是mq管理器中消息数量没增长
看服务器上的mq配置,看看mq_log,是不是存储满了?
- 前端/html5效果收藏
H5应用 9款漂亮的H5效果 8款漂亮的H5效果 36漂亮的button效果 颜色RGB表 省市二级联动
- GET方式,获取服务器文件
package com.http.get; import java.io.FileOutputStream; import java.io.IOException; import java.io.In ...
- HTML5 web开发时遇到的一个奇葩问题。
昨天做了一个手机端的H5 页面. 首先就是各种兼容测试,调整修复..一系列操作之后,拿过来N多手机神马华为.小米.三星.水果5.6.plus,一番测试之后.嗯,还不错,稍作等待之后就上线了. 这是分割 ...
- Android 访问权限设置
Android开发应用程序时,有时我们要用到很多权限, 今天我就收集了一些开发时可能用到的开启权限设置. 这些权限都是在 AndroidManifest.xml设置. 设置方法 <uses-pe ...
- WINDOW下php开启pgsql拓展
操作步骤: 1.修改php.ini,去掉“extension=php_pgsql.dll ”和“extension=php_pdo_pgsql.dll ”前的分号.2.确认C:\php\ext\下ph ...
- MySQL 插入数据时,中文乱码???问题的解决
在终端,mysql -u root -p 登录: show variables like 'character%'; 来查看当前数据库的相关编码集. client 为客户端使用的字符集. connec ...
- Struts2请求处理流程及源码分析
1.1 Struts2请求处理 1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionConte ...
- 采访:Go语言编程
Go语言是由Google在2009年11月份公布的,它的目标是要应对软件开发所面临的最新挑战.Go语言特别被设计为快速(包括在编译时).支持多核的语言,并且兼顾了动态语言的简单性和静态类型语言的安全性 ...
- HP Webinspect 10 访问wap的url
HP Webinspect是著名的扫描工具,这里讲一下怎么使用它扫wap的url. 通俗的讲,Wap是手机网页浏览器使用的网页,web是电脑网页浏览器使用的网页.(讲得不专业,但方便理解) 在手机上显 ...