使用pywin32处理excel文件
#!/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()
使用pywin32处理excel文件的更多相关文章
- Python笔记:使用pywin32处理excel文件
因为前端同事须要批量的对excel文件做特殊处理,删除指定行,故写了此脚本.同一时候配合config.ini方便不熟悉py的同事使用 #!/usr/bin/env python #-*- coding ...
- 用Python读写Excel文件(转)
原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- Python将excel文件从xls转换为xlsx
本文使用场景:将一个xls格式Excel文件转换为xlsx文件格式.接下来将一步一步演示该操作.你也可以对代码进行修改使其适用于你所需的场景. 安装Python3 首先需要安装Python,我这里安装 ...
- 用Python读写Excel文件的方式比较
虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...
- Python: 如何判断远程服务器上Excel文件是否被人打开
最近工作中需要去判断远程服务器上的某个Excel文件是否被打开,如果被人打开,则等待,如果没人打开使用,则去填写数据进Excel文件. 开始想的很简单,和其他语言一样,比如C#,打开文件,如果报错说明 ...
- 用php生成一个excel文件(原理)
1.我们用php来生成一个excel文档来讲述其原理: excel2007里面的文档目录组成部分为: 2.我们使用ZipArchive()方法来生成一个简易的excel文件. 使用方法: 3.代码如下 ...
- ASP.NET MVC5下载数据到Excel文件
项目中的一个功能是将数据导入到Excel文件中,这里使用NPOI操作Excel,代码如下: public class Excel : IDataTransfer { public Stream Exp ...
- Python 操作 MS Excel 文件
利用 Python 对 Excel 文件进行操作需要使用第三方库: openpyxl,可执行 pip install openpyxl 进行安装 1. 导入 openpyxl 模块 导入 openpy ...
随机推荐
- BaseControl按钮合集
BaseControl按钮合集 效果 源码 https://github.com/YouXianMing/Animations // // POPBaseControl.h // Animations ...
- JDK配置 linux
在启动终端并输入 gedit /etc/profile 在末尾添加一下配置,保存并退出 #set jdk environment export JAVA_HOME=/usr/lib/jvm/jdk1. ...
- JDK(Java SE Development Kit)的安装与环境变量的配置
本文参考于:http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html 感谢作者的贡献~ 首先,进入网址下载JDK:http://ww ...
- Orchard之创建列表
一:首先需要确保 List Module 的开始 即: Enable 之后,左边的列表中,多了一个 List 功能菜单. 二:为 Content type 选定 Cotainable 不再赘述. 三: ...
- [转]Linux常用命令大全
From : http://www.php100.com/html/webkaifa/Linux/2009/1106/3485.html 系统信息 arch 显示机器的处理器架构(1) uname - ...
- 使用Bootstrap后,关于IE与Chrome显示字体的问题
在做日志系统时,使用了Bootstrap,然后通过浏览器查看的页面效果如下 对比可以看到,同样的字体,IE显示的圆润些,而Chrome字体则丑很多.因为Chrome默认用宋体 在http://v3.b ...
- Xen4CentOS 帮你移植到 CentOS 6 和 Xen 4
CentOS 发布了 Xen4CentOS 项目,该项目的目的是为了帮助 CentOS 5 的 Xen 用户移植到 CentOS 6,同时更新到 Xen 4 .因为 RHEL 6 不再提供 Xen,改 ...
- AutoCppHeader AutoHeader 自动根据CPP 或C文件 来生成头文件。
根据 cpp文件 生成相应的头文件 namespace 声明类定义声明 函数声明 extern 变量声明 选项--------------//[AutoHeader Public] //[AutoHe ...
- Arcgis ArcMap 10 如何生成msd地图文档定义【 arcgis mxd怎么转换成msd】
.mxd是arcgis 的地图文档后缀名. .msd是arcgis 的地图服务定义文件,是 map service definition 的缩写. 基于 MSD 的服务支持 Maplex.制图表达和新 ...
- opengl 教程(24) shadow mapping (2)
原帖地址:http://ogldev.atspace.co.uk/www/tutorial24/tutorial24.html 本篇教程中,我们通过shadowmap来实现阴影渲染. 我们知道shad ...