Python办公自动化案例实战

2023-12-22 22:09:38 浏览数 (1)

⾃动化办公包括内容:

1. 基本⽂件操作,⽐如批量操作txt、csv、excel、word、pdf等

2. 这⾥操作包括:⽂件打开、读取、写⼊

我们先从哪⾥讲起?先从基本的⽂件查找、分类开始。

⽐如查找某个⽂件,在某个⽂件夹⾥,还包括从⽂件⽬录(也就是包括多个⽂件夹);查找指定

后缀名的所有⽂件

还包括哪些任务:从⼀个⽬录剪切或复制某个⽂件到另⼀个⽂件,剪切或复制某个⽂件夹到另⼀

个⽬录

掌握这些基本的⽂件操作后,我们将开始从操作txt⽂件开始,包括打开、读取、写⼊,从指定⾏

开始读取,分⽚读取⼤⽂件等

然后再讲解操作excel⽂件,这是⽐较常⽤的⼀类⽂件

然后再讲解Python操作word⽂档

最后再讲解操作pdf⽂档

1 某个⽂件夹下查找某个⽂件

请下载资料包,并在Pycharm中运⾏程序

代码语言:python代码运行次数:0复制
# 连续在res⽂件夹内查找某个⽂件
import os
# 获取当前路径
cur_dir = os.path.dirname(os.path.abspath(__name__))
# 获取当前路径⽗⽬录
parent_dir = os.path.dirname(cur_dir)
# 定位到res⽂件夹
find_path = os.path.join(parent_dir, 'res')
while 1:
 find_file_name = input(f"请提示你想在{find_path}中查找的⽂件名:(按q退出) ")
 if find_file_name == 'q':
 break
 file_exists = False # 表示是否存在
 for item in os.listdir(find_path):
 if item == find_file_name:
 file_exists = True
 break
 if file_exists:
 print("⽂件找到")
 else:
 print("⽂件未找到")

2 递归查找在res⽂件夹内查找某个⽂件

代码语言:python代码运行次数:0复制
import os
# 获取当前路径
cur_dir = os.path.dirname(os.path.abspath(__name__))
# 获取当前路径⽗⽬录
parent_dir = os.path.dirname(cur_dir)
# 定位到res⽂件夹
find_path, file_path = os.path.join(parent_dir, 'res'), None
while 1:
 find_file_name = input(f"请提示你想在{find_path}中查找的⽂件名:(按q退出) ")
 if find_file_name == 'q':
 break
 file_exists = False # 表示是否存在
 for root, dirs, files in os.walk(find_path):
 for item in files:
 if item == find_file_name:
 file_exists = True
 file_path = os.path.join(root, item)
 break
 if file_exists:
 break
 if file_exists:
 print(f"在路径:{file_path}中找到⽂件")
 else:
 print("⽂件未找到")

3 根据⽂件后缀名查找⽂件

代码语言:python代码运行次数:0复制
# 第⼀件事情,os.path.splitext()中传⼊item(⽂件名是**.&&&】)
# 这时,返回的结果是列表:["**", ".&&&"]
# [1]表示选择上⾯列表中,下标为1的元素,即选出".&&&"
# 第三件事情,lower()函数将".&&&"中的⼤写英⽂字⺟统⼀转为⼩写
# 这时的extension赋值的结果为".docx",字⺟是⼩写
import os
# 获取当前路径
cur_dir = os.path.dirname(os.path.abspath(__name__))
# 获取当前路径⽗⽬录
parent_dir = os.path.dirname(cur_dir)
extension_name = input(f"你想要查找哪写后缀名⽂件,不同⽂件类型间⽤,隔开(如xlsx,mp3等
tmp_extensions = extension_name.split(",")
extensions = ['.'   item for item in tmp_extensions]
found_files_count = 0
for item in os.listdir(os.path.join(parent_dir, 'res')):
 extension = os.path.splitext(item)[1].lower()
 if extension in extensions:
 print(f"【找到⽂件】"   item)
 found_files_count  = 1
print(f"⼀共有{found_files_count}个⽂件")

4 归移动到⽂件

代码语言:python代码运行次数:0复制
import os
import shutil
source_folder_name = input("输⼊想要操作的⽂件夹:")
file_extension = input("输⼊想要复制⽂件后缀(格式为xlsx等)")
target_folder = input("输⼊移动到⽂件夹名称")
mv_files = []
for root, folders, files in os.walk(source_folder_name):
 for cur_file in files:
 if os.path.splitext(cur_file)[1] == '.'   file_extension:
 mv_files.append(os.path.join(root, cur_file))
# 剪切这些⽂件
if input('你确定要移动吗?(y/n)') == 'y':
 for cp_file in mv_files:
 if os.path.exists(os.path.join(target_folder, os.path.basename(cp_file
 continue
 shutil.move(cp_file, target_folder)
 print("Done")

5 递归复制⽂件

代码语言:python代码运行次数:0复制
import os
import shutil
source_folder_name = input("输⼊想要操作的⽂件夹:")
file_extension = input("输⼊想要复制⽂件后缀(格式为xlsx等)")
target_folder = input("输⼊复制到⽂件夹名称")
cp_files = []
for root, folders, files in os.walk(source_folder_name):
 for cur_file in files:
 if os.path.splitext(cur_file)[1] == '.'   file_extension:
 cp_files.append(os.path.join(root, cur_file))
# 复制这些⽂件
for cp_file in cp_files:
 shutil.copy(cp_file, target_folder)
print("Done")

6 递归删除

代码语言:python代码运行次数:0复制
import os
source_folder_name = input("输⼊想要操作的⽂件夹:")
file_extension = input("输⼊想要删除⽂件后缀(格式为xlsx等)")
del_files = []
for root, folders, files in os.walk(source_folder_name):
 for cur_file in files:
 if os.path.splitext(cur_file)[1] == '.'   file_extension:
 del_files.append(os.path.join(root, cur_file))
# 删除这些⽂件
if input('你确定要删除吗?(y/n)') == 'y':
 for cp_file in del_files:
 os.remove(cp_file)
 print("Done")

7 整理为模块

代码语言:python代码运行次数:0复制
import os
import shutil
def copy_files(source_folder_name, target_folder, file_extension='xlsx'):
 """
 批量复制指定后缀名的⽂件
 :param source_folder_name:
 :param target_folder:
 :param file_extension:
 :return:
 """
 cp_files = []
 for root, folders, files in os.walk(source_folder_name):
 for cur_file in files:
 if os.path.splitext(cur_file)[1] == '.'   file_extension:
 cp_files.append(os.path.join(root, cur_file))
 # 复制这些⽂件
 for cp_file in cp_files:
 shutil.copy(cp_file, target_folder)
 print("Done")
def delete_files(source_folder_name, file_extension='xlsx'):
 """
 批量删除指定后缀名的⽂件
 :param source_folder_name:
 :param file_extension:
 :return:
 """
 del_files = []
 for root, folders, files in os.walk(source_folder_name):
 for cur_file in files:
 if os.path.splitext(cur_file)[1] == '.'   file_extension:
 del_files.append(os.path.join(root, cur_file))
 # 删除这些⽂件
 for cp_file in del_files:
 os.remove(cp_file)
 print("Done")
def move_files(source_folder_name, target_folder, file_extension='xlsx'):
 """
 批量移动指定后缀名的⽂件
 :param source_folder_name:
 :param target_folder:
 :param file_extension:
 :return:
 """
 mv_files = []
 for root, folders, files in os.walk(source_folder_name):
 for cur_file in files:
 if os.path.splitext(cur_file)[1] == '.'   file_extension:
 mv_files.append(os.path.join(root, cur_file))
 # 剪切这些⽂件
 for cp_file in mv_files:
 if os.path.exists(os.path.join(target_folder, os.path.basename(cp_file
 continue
 shutil.move(cp_file, target_folder)
 print("Done")
def walk_to_find(find_path, find_file_name):
 """
 路径find_path中递归查找⽂件find_file_name
 :param find_path:
 :param find_file_name:
 :return:
 """
 result_path = []
 for root, dirs, files in os.walk(find_path):
 for item in files:
 if item == find_file_name:
 file_path = os.path.join(root, item)
 result_path.append(file_path)
 if len(result_path) > 0:
 print("找到⽂件")
 print("n".join(result_path))
 else:
 print("⽂件未找到")
 return result_path
def find_extension(find_path, extension_name='xlsx,mp3'):
 """
 递归查找指定后缀名的⽂件
 :param find_path:
 :param extension_name:
 :return:
 """
 tmp_extensions = extension_name.split(",")
 extensions = ['.'   item for item in tmp_extensions]
 result_path = []
 for cur_dir, folders, files in os.walk(find_path):
 for item in files:
 extension = os.path.splitext(item)[1].lower()
 if extension in extensions:
 result_path.append(os.path.join(cur_dir, item))
 if len(result_path) > 0:
 print("n".join(result_path))
 else:
 print("未找到")
def stat_file(folder_name):
 """
 统计⽂件个数,⽂件夹个数
 :param folder_name:
 :return:
 """
 file_count, folder_count = 0, 0
 if folder_name:
 for dir, folders, files in os.walk(folder_name):
 file_count  = len(files)
 folder_count  = len(folders)
 print(f"{folder_name}⾥⼀共包括:{file_count}个⽂件, {folder_count}个⽂件夹
 return file_count, folder_count

【小结】

以上是Python办公自动化的常见玩法。里面的窍门还是有一些的,没有特别的技巧,唯手熟尔。

我正在参与2023腾讯技术创作特训营第四期有奖征文,快来和我瓜分大奖!

0 人点赞