zl程序教程

您现在的位置是:首页 >  后端

当前栏目

自动化之Python 中自动执行大部分 Excel 任务的函数

PythonExcel自动执行自动化 函数 任务 大部分
2023-09-11 14:18:47 时间

我是一名模型风险分析师,我的日常工作涉及处理模型库存数据的大量治理,为此我最终使用 excel 表和 word 文档来处理任何事情。今天我将讨论一些我用来在 Python 中自动执行大部分 Excel 任务的函数。让我们开始!

1. 使用 Glob 库合并文件和文件夹

Python ‘os, shutil & glob’ 模块是文件处理活动的有效方式。以下是管理您正在使用的目录的一些基本方法:

import os 
import shutil
import glob
#return current working directory(cwd)
cwd = os.getcwd()
#change cwd location
os.chdir('/Users/sammitiyadav/Desktop')
#add a folder in the cwd 
os.mkdir('new folder')
#delete folder from the cwd
os.rmdir('new folder')
#create paths without having to use slashes 
os.path.join(cwd, 'new folder')
#check if the directory exists 
os.path.isdir(cwd) #returns true if it does and false otherwise
#change paths(location) of a file 
#os.rename(old path, new path)
os.rename('test.xlsx', os.path.join(cwd, 'new folder', 'test.xlsx'))
shutil.move('test.xlsx', os.path.join(cwd, 'new folder', 'test.xls