python 办公自动化# 1 文件自动化处理
Posted Gendan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 办公自动化# 1 文件自动化处理相关的知识,希望对你有一定的参考价值。
python创建并写入新文件,
python统计特定文件夹下的word和pdf的数量
import glob,os
path就是你说的特定文件夹
path = r"D:\\linshi"
这里的pdf可以换成docx
file=glob.glob(os.path.join(path, "*.pdf"))
count = 0
for i in file:
count = count + 1
print(count)
复制文件的完整路径借助python对该文件夹的文件批量复制到另一个指定文件夹中。有两种模式,一种只复制文件。第二种复制文件的完整路径
import os
import shutil
def get_all_file_by_type(path, type=()): # 获得以type类型结尾的所有文件,返回一个list
filelist = []
for a, b, c in os.walk(path):
for name in c:
fname = os.path.join(a, name)
if fname.endswith(type):
filelist.append(fname)
return filelist
def get_all_file_by_string(path, string_list):
filelist = []
for a, b, c in os.walk(path):
for name in c:
fname = os.path.join(a, name)
for string in string_list: # 遍历string_list,如果文件路径中包含string,那么append进filelist
if string in fname: # 如果只想要文件名符合条件,把fname换成name即可
filelist.append(fname)
break
return filelist
def copy_file_by_type(old_path, new_path, type=(\'doc\', \'docx\'), requird_dir=False):
try:
file_list = get_all_file_by_type(old_path, type=type) # 获得该路径下所有的type类型文件
if not os.path.exists(new_path): # 创建新的文件夹
os.makedirs(new_path)
if not requird_dir: # 如果仅复制文件
for file in file_list:
name = file.split("\\\\")[-1] # 获得文件名字
new_paths = os.path.join(new_path, name) # 与新路径拼接,获得完整的新路径
shutil.copy(file, new_paths)
print(new_paths + "成功")
if requird_dir:
for file in file_list:
name = file.split("\\\\")[-1] # 获得文件名字
new_paths = file.replace(old_path, new_path) # 将一个完整路径中,开始的路径替换成新的路径
dir = new_paths.split(name)[0] [PerfectMoney下载](https://www.gendan5.com/wallet/PerfectMoney.html)# 获得文件夹路径
if not os.path.exists(dir): # 创建新文件夹
os.makedirs(dir)
shutil.copy(file, new_paths)
print(new_paths + "成功")
except Exception as e:
print(e)
def copy_file_by_string(old_path, new_path, string_list, requird_dir=False):
try:
file_list = get_all_file_by_string(old_path, string_list=string_list) # 与上述一样,只不过这里调用的是get_all_file_by_string方法
if not os.path.exists(new_path):
os.makedirs(new_path)
if not requird_dir:
for file in file_list:
name = file.split("\\\\")[-1]
new_paths = os.path.join(new_path, name)
shutil.copy(file, new_paths)
print(new_paths + "成功")
if requird_dir:
for file in file_list:
name = file.split("\\\\")[-1]
new_paths = file.replace(old_path, new_path)
print(new_paths)
dir = new_paths.split(name)[0]
if not os.path.exists(dir):
os.makedirs(dir)
shutil.copy(file, new_paths)
print(new_paths + "成功")
except Exception as e:
print(e)
if name == \'__main__\':
old_path = r"F:\\aaaa"
new_path = r"F:\\bbbb"
list = ["面试", "笔试", "题库", "题目"]
copy_file_by_string(old_path=old_path, new_path=new_path, string_list=list, requird_dir=False)
# type = (\'docx\',\'doc\',"pdf","md")
# copy_file_by_type(old_path=old_path, new_path=new_path, type=type, requird_dir=True)
python压缩多个文件到zip格式-zipfile包实例
pip install zipfile
file=r\'D:\\test.zip\'
out_path=r\'D:\\files\'
遍历files文件夹下的文件,压缩发送
zip_1=zipfile.ZipFile(file,\'w\')
for f in os.listdir(out_path):
zip_1.write(os.path.join(out_path,f),f,zipfile.ZIP_DEFLATED)
zip_1.close()
python批量删除文件名_Python批量修改文件名
import os, re
while True:
keyword = input("请输入你要删除的字符串:")
if len(keyword)==0 or keyword.isspace():
print("字符串不能为空!")
else:
break
suffix = input("需要筛选的文件名后缀(Enter代表所有):")
fileNames = os.listdir() #获取当前目录下的所有文件
for file in fileNames:
check = os.path.join(os.path.abspath(\'.\'),file)
if os.path.isfile(check):
if len(suffix)==0 or suffix.isspace():
if keyword in file:
print(file," -> ",file.replace(keyword,\'\'))
os.rename(file,file.replace(keyword,\'\'))
else:
用正则表达式匹配后缀名
if re.match(\'.+?.\'+suffix+\'$\',file) != None and keyword in file:
print(file," -> ",file.replace(keyword,\'\'))
os.rename(file,file.replace(keyword,\'\'))
以上是关于python 办公自动化# 1 文件自动化处理的主要内容,如果未能解决你的问题,请参考以下文章
Python自动化办公批量将文字写入Word文件中的具体操作