前几天在Python白银群【由恒远】问了一个Python
自动化办公处理的问题,这里拿出来给大家分享下。
他自己的代码如下:
import os
import shutil
import glob
# 指定源目录和目标目录
source_dir = r"D:\设计类工作资料"
target_dir = r"D:\xx"
#获取源目录下所有Excel文件的文件名
excel_files = glob.glob(os.path.join(source_dir, "*.xlsx"))
# 将源目录下的所有Excel文件复制到目标目录中
for file in excel_files:
shutil.copyfile(file, os.path.join(target_dir, file))
# 将库文件复制到目标目录中
library_files = glob.glob(os.path.join(source_dir, "*.pyd"))
for file in library_files:
shutil.copyfile(file, os.path.join(target_dir, file))
这里【Python进阶者】给了一个代码,这个代码可以直接复制.xlsx和.xls的文件,相当于全部的excel数据表了。
import shutil
import os
def copy_file(path):
# (root,dirs,files)分别为:遍历的文件夹,遍历的文件夹下的所有文件夹,遍历的文件夹下的所有文件
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.xlsx') or file.endswith('.xls'):
# if "_双章" in file: # 多了一层限定条件
# shutil.copyfile(root + '\\' + file, target_path + '\\' + file) # 复制
shutil.move(root + '\\' + file, target_path + '\\' + file) # 剪切
print(root + '\\' + file + ' 复制成功-> ' + target_path)
for dir_in in dirs:
copy_file(dir_in)
if __name__ == '__main__':
# 文件夹路径
source_path = r'C:\Users\Desktop\test\收入确认表\sample'
# 输出路径
target_path = r'C:\Users\Desktop\test\收入确认表\收入确认表pdf文件'
copy_file(source_path)
这个代码适用性还是很强的,可以自己修改,比方说移动pdf或者其他标识的文件等,都可以的。
大家好,我是皮皮。这篇文章主要盘点了一个Python
自动化办公处理的问题,文中针对该问题,给出了具体的解析和代码实现,帮助粉丝顺利解决了问题。
本文分享自 Python爬虫与数据挖掘 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有