python實現Excel刪除特定行、拷貝指定行操作


工作中遇到的,本來用VBA寫的,操作很慢,嘗試用Python實現,

任務需求:

從原始的兩張表中拷貝行到五張表中,如下表所示:

source1和source2是一樣的格式:

        one two three four five
1 2 3   11 11 22 22 33 33 44 44 55 55

目標表格有one,two,three,four,five。

將前三列和對應表的兩列拷貝到目標表格中,目標表格中原始也需要刪除有關鍵字的行(注釋代碼中實現了,但是有8000多行,刪除很耗時,改為了手動刪除),然后把source1和source2含有同樣關鍵字的行拷貝過來,拷貝的列索引是固定的

實現:

采用xlrd讀Excel,xlutils寫Excel,注釋中用win32com刪除行(需要安裝模塊pip install pywin32)

#! /usr/bin/env python
# encoding:utf-8
import os
import xlrd
from xlutils.copy import copy
import win32com.client as win32
'''
文件名、修改的表名、查找的字符串固定,如有變化,則相應修改
'''
regstr = ['str1', 'str2']
tarExcels = ['one.xls','two.xls','three.xls','four.xls','five.xls']
tarSheet = 'targetSheet'
sourceExcels =['source1.xlsm','source2.xlsm']

def copyFeatrue(sourcefiles,targetfiles):
    for item in sourcefiles:
        workbook = xlrd.open_workbook(item)
        shEng = workbook.sheet_by_index(0)
        rowNum = shEng.nrows
        '''從原始中英文表中提取出要放入五張表的內容'''
        ListMacro =[]
        ListMICRO =[]
        ListATOM =[]
        List3205E =[]
        List3911E =[]
        startRowIdx = 0
        for row in range(rowNum):
            if shEng.cell(row,4).value in regstr:
                break
            startRowIdx += 1
        for rowIdx in range(startRowIdx,rowNum):
            commstr =[]
            tempMacro = []
            tempMICRO = []
            tempATOM = []
            temp3205E = []
            temp3911E = []
            '''前三列公共,后面五張表各自取不同的列'''
            commstr.append(shEng.cell(rowIdx,0).value)
            commstr.append(shEng.cell(rowIdx,1).value)
            commstr.append(shEng.cell(rowIdx,2).value)

            if shEng.cell(rowIdx,4).value:
                tempMacro.extend(commstr)
                tempMacro.append(shEng.cell(rowIdx,4).value)
                tempMacro.append(shEng.cell(rowIdx, 5).value)

            if shEng.cell(rowIdx, 8).value:
                tempMICRO.extend(commstr)
                tempMICRO.append(shEng.cell(rowIdx, 8).value)
                tempMICRO.append(shEng.cell(rowIdx, 9).value)

            if shEng.cell(rowIdx, 10).value:
                tempATOM.extend(commstr)
                tempATOM.append(shEng.cell(rowIdx, 10).value)
                tempATOM.append(shEng.cell(rowIdx, 11).value)

            if shEng.cell(rowIdx, 12).value:
                temp3205E.extend(commstr)
                temp3205E.append(shEng.cell(rowIdx, 12).value)
                temp3205E.append(shEng.cell(rowIdx, 13).value)

            if shEng.cell(rowIdx, 14).value:
                temp3911E.extend(commstr)
                temp3911E.append(shEng.cell(rowIdx, 14).value)
                temp3911E.append(shEng.cell(rowIdx, 15).value)
            if tempMacro:
                ListMacro.append(tempMacro)
            if tempMICRO:
                ListMICRO.append(tempMICRO)
            if tempATOM:
                ListATOM.append(tempATOM)
            if temp3205E:
                List3205E.append(temp3205E)
            if temp3911E:
                List3911E.append(temp3911E)

        '''表名和抽取出的內容一一對應'''
        dic ={}
        dic[tarExcels[0]] = List3911E
        dic[tarExcels[1]] = List3205E
        dic[tarExcels[2]] = ListATOM
        dic[tarExcels[3]] = ListMICRO
        dic[tarExcels[4]] = ListMacro
        realfile =''

        '''通過表名查找到對應的表的絕對路徑,以便讀取'''
        for j in range(5):
            for fileidx in range(len(targetfiles)):
                if tarExcels[j] in targetfiles[fileidx]:
                    realfile = targetfiles[fileidx]
                    break
            workdest1 = xlrd.open_workbook(realfile)
            shdest1 = workdest1.sheet_by_name(tarSheet)
            rows = shdest1.nrows
            targetlist = dic[tarExcels[j]]
            '''創建新表,將對應內容寫入對應表中'''
            newbook = copy(workdest1)
            newsheet = newbook.get_sheet(tarSheet)
            listidx = 0
            '''寫入表的位置固定,根據列索引寫入'''
            for r in range(rows,rows+len(targetlist)):
                newsheet.write(r,0,targetlist[listidx][0])
                newsheet.write(r, 1, targetlist[listidx][1])
                newsheet.write(r, 2, targetlist[listidx][2])
                newsheet.write(r, 4, targetlist[listidx][3])
                newsheet.write(r, 5, targetlist[listidx][4])
                listidx += 1
            newbook.save(realfile)

if __name__ == '__main__':
    print('Running! Please Wait!\n')
    targetfiles =[]
    sourcefiles =[]
    '''遍歷腳本所在目錄下所有文件,並且找出與目標文件一致的文件的絕對路徑'''
    for root,dirs,files in os.walk(os.getcwd()):
        for name in files:
            if name in tarExcels:
                targetfiles.append(os.path.join(root, name))
            if name in sourceExcels:
                sourcefiles.append(os.path.join(root, name))
    copyFeatrue(sourcefiles,targetfiles)
    print('^_^Success!^_^')
    input('input any key to continue!\n')

'''
class copyExcel:
    def __init__(self,filename = None):
        self.workApp = win32.Dispatch('Excel.Application')
        if filename:
            self.filename = filename
            self.workbook = self.workApp.Workbooks.Open(filename)
        else:
            self.workbook = self.workApp.Workbooks.Add()
            self.filename = ''

    def deleteRow(self,sheet,row):
        sht = self.workbook.Worksheets(sheet)
        sht.Rows(row).Delete()

    def save(self, newfile=None):
        if newfile:
            self.filename = newfile
            self.workbook.SaveAs(newfile)
        else:
            self.workbook.Save()

    def close(self):
        self.workbook.Close(SaveChanges = 0)
        del self.workApp

    def getCell(self,sheet,row,col):
        sht = self.workbook.Worksheets(sheet)
        return sht.Cells(row,col).Value

    def setCell(self,sheet,row,col,value):
        sht = self.xlBook.Worksheets(sheet)
        sht.Cells(row, col).Value = value
    def getRowNum(self,sheet):
        sht = self.workbook.Worksheets(sheet)
        return sht.usedrange.rows.count

##刪除目標字符串對應的行
def delFeature(destdir):
    for i in range(len(tarExcels)):
        sourfile = destdir + '\\' + tarExcels[i]
        temp = copyExcel(sourfile)
        #workbook = win32.Dispatch('Excel.Application').Workbooks.Open('D:\py\chen\liu.xlsx')
        rowNum = temp.getRowNum(tarSheet)
        print(rowNum)
        row = 1
        while row <= rowNum:
            #print(row)
            if temp.getCell(tarSheet,row,5) == 'str1' or temp.getCell(tarSheet,row,5) == 'str2':
                temp.deleteRow(tarSheet,row)
                row -= 1
                rowNum -=1
            row += 1
        temp.save()
        temp.close()

'''

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM