工作中遇到的,本来用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
&#

本文介绍如何使用Python高效地实现Excel数据处理,包括从源Excel拷贝特定行到多个目标表格,以及删除包含特定关键字的行。通过xlrd、xlutils库进行读写操作,替代VBA实现快速处理8000多行数据。
最低0.47元/天 解锁文章
8088

被折叠的 条评论
为什么被折叠?



