1.想用win32.com 的copy方法,实现遍历指定文件夹里所有工作表,排除指定的工作表后,其他工作表都批量,复制到指定的一个工作簿中
目前只会手动一个个复制,不会遍历文件夹
望大神帮助
参考:https://blog.csdn.net/m0_38082783/article/details/113767766?spm=1001.2014.3001.5502
```python
import os
import xlwings as xw
app = xw.App(visible=False,add_book=False)
file_path = 'd:\\python_file\\销售表' # 给出目标工作簿所在的文件夹路径
file_list = os.listdir(file_path) # 列出文件与子文件夹
workbook = app.books.open('d:\\python_file\\信息表.xlsx') # 打开来源工作表
worksheet = workbook.sheets # 获取来源工作簿中的所有工作表
for i in file_list:
if os.path.splitext(i)[1] == '.xlsx': # 判断文件是否是工作簿
workbooks = app.books.open(file_path + '\\' + i) # 如果是则打开
for j in worksheet:
contents = j.range('A1').expand('table').value # 读取来源工作簿中要复制的工作表数据
name = j.name # 获取来源工作簿中的工作表名称
workbooks.sheets.add(name=name, after=len(workbooks.sheets)) # 在目标工作簿中新增同名工作表
workbooks.sheets[name].range('A1').value = contents # 将从来源工作簿中读取的工作表数据写入新增工作表
workbooks.save()
app.quit()
```
不难的,直接用sdk的api也可以。
#-- coding: utf8 --
import os
import shutil
sorcepath='D:/Mypython/test/'
targepath='C:/'
removefilename='a.txt'
files = os.listdir(sorcepath)
for file in files:
if not os.path.isdir(file) and file.find(removefilename)== -1:
print(os.path.join(sorcepath+file))
print(os.path.join(targepath+file))
shutil.copy(os.path.join(sorcepath+file),os.path.join(targepath+file))
遍历文件夹,采用基础的shell操作应该就可以
python os 方法,遍历文件夹,写相关逻辑可实现