Python实现批量生成,重命名和删除word文件

批量生成word文件

场景:需要新建多个类似文件名

比如:今天的事例是新建12个文件名为:

保安员考试试卷1及答案.docx

保安员考试试卷2及答案.docx

​ …

保安员考试试卷12及答案.docx

痛点:

手动操作重复性高,出错率高,易造成人疲劳,降低效率;

安装

  1. pip install pythondocx

log

(env_qt) D:\code\qt_demo\demo>pip install python-docx
Looking in indexes: pypi.tuna.tsinghua.edu.cn/simple
Collecting python-docx
Using cached pypi.tuna.tsinghua.edu.cn/packages/8b…
33/python-docx-0.8.11.tar.gz (5.6 MB)
Preparing metadata (setup.py) … done
Collecting lXML>=2.3.2
Downloading pypi.tuna.tsinghua.edu.cn/packages/39…
8/lxml-4.9.2-cp39-cp39-win_amd64.whl (3.9 MB)
—————————————- 3.9/3.9 MB 1.4 MB/s eta 0:00:00
Building wheels for collected packages: python-docx
Building wheel for python-docx (setup.py) … done
Created wheel for python-docx: filename=python_docx-0.8.11-py3-none-any.whl size=184519 sha256=595cd9888a3832964d8f2477f
fd5325f596549da8c1dd305e63d5f7b3d24884c
Stored in directory: c:\users\wz\appdata\local\pip\cache\wheels\39\ca\c1\d3e7abe5ce2e8423382d816e689c056bc26590f48fad8f2
0ac
Successfully built python-docx
Installing collected packages: lxml, python-docx
Successfully installed lxml-4.9.2 python-docx-0.8.11
[notice] A new release of pip available: 22.3.1 -> 23.0.1
[notice] To update, run: python.exe -m pip install –upgrade pip
(env_qt) D:\code\qt_demo\demo>****

官方文档:

https://python-docx.readthedocs.io/en/latest/index.html

三方库引入

  1. import os
  2. from docx import Document

解释:

1.在文件路径下会获取文件夹下面所有文件,使用os模块;

2.word文档的相关操作使用docx模块;

批量新建文件名

场景:需要新建多个类似文件名

比如:今天的事例是新建12个文件名为:

保安员考试试卷1及答案.docx

保安员考试试卷2及答案.docx

​ …

保安员考试试卷12及答案.docx

痛点:

手动操作重复性高,出错率高,易造成人疲劳,降低效率;

  1. def gen_names(pre_fix, num):
  2.      “””
  3.      批量生成文件名
  4.      :param pre_fix: 文件名的前缀
  5.      :param num: 文件数量
  6.      :return:name_list :文件名列表(所有要生成的文件名)
  7.      “””
  8.      print(“生成文件名列表开始”)
  9.  
  10.      name_list = []
  11.      for i in range(1, num + 1):
  12.          tmp = pre_fix + str(i)
  13.          name_list.append(tmp)
  14.      print(“生成文件名列表结束”)
  15.      return name_list

生成word文件

生成单个文件

  1. def new_one_file(document, name):
  2.      “””
  3.      生成单个文件
  4.      :param document:
  5.      :param name: 文件名
  6.      :return:
  7.      “””
  8.      print(“单个生成文件开始”)
  9.      document.save(name + ‘.docx’)
  10.      print(“单个生成文件结束”)

生成多个文件

  1. def new_many_file(document, names):
  2.      “””
  3.      生成多个文件
  4.      :param document: Document实例对象
  5.      :param names:多个文件的文件名
  6.      :return:
  7.      “””
  8.      print(“批量生成文件开始”)
  9.      for name in names:
  10.          document.save(os.path.join(“tmp”, name + ‘.docx’))
  11.      print(“批量生成文件结束”)

重命名文件

  1. def rename_file(path):
  2.      “””
  3.      将path路径下的文件都重命名
  4.      规则:在原文件名后面加上 —> “及答案.” 字段
  5.      :param path:
  6.      :return:
  7.      “””
  8.      print(“重命名开始”)
  9.      files = os.listdir(path)
  10.      for file in files:
  11.          name, doc_type = file.split(“.”)
  12.          new_name = os.path.join(path, name + “及答案.” + doc_type)
  13.          file_tmp = os.path.join(path, file)
  14.          # 语法:
  15.          # os.rename(src, dst) :用于命名文件或目录
  16.          # src:需要修改的文件或目录名。
  17.          # dst:修改后的文件或目录名。
  18.          os.rename(file_tmp, new_name)
  19.      print(“重命名结束”)

删除文件

  1. def del_files(path):
  2.      “””
  3.      删除指定路径下的文件
  4.      :param path:
  5.      :return:
  6.      “””
  7.      print(“删除开始”)
  8.      files = os.listdir(path)
  9.      for file in files:
  10.          os.remove(os.path.join(path, file))
  11.      print(“删除成功”)

函数调用

  1. if __name__ == ‘__main__’:
  2.      path = r“C:\Users\wz\Desktop\保安员考试试卷”
  3.      document = Document()
  4.      pre_fix = “保安员考试”
  5.      # name_all = gen_names(pre_fix, 10)
  6.      # new_many_file(document, name_all)
  7.      # rename_file(“tmp”)
  8.      del_files(“tmp”)

效果展示

批量生成文件

-1

批量重命名文件

-2

删除文件

-3

所有代码

  1. import os
  2. from docx import Document
  3.  
  4.  
  5. def gen_names(pre_fix, num):
  6.      “””
  7.      批量生成文件名
  8.      :param pre_fix: 文件名的前缀
  9.      :param num: 文件数量
  10.      :return:name_list :文件名列表(所有要生成的文件名)
  11.      “””
  12.      print(“生成文件名列表开始”)
  13.  
  14.      name_list = []
  15.      for i in range(1, num + 1):
  16.          tmp = pre_fix + str(i)
  17.          name_list.append(tmp)
  18.      print(“生成文件名列表结束”)
  19.      return name_list
  20.  
  21.  
  22. def new_one_file(document, name):
  23.      “””
  24.      生成单个文件
  25.      :param document:
  26.      :param name: 文件名
  27.      :return:
  28.      “””
  29.      print(“单个生成文件开始”)
  30.      document.save(name + ‘.docx’)
  31.      print(“单个生成文件结束”)
  32.  
  33.  
  34. def new_many_file(document, names):
  35.      “””
  36.      生成多个文件
  37.      :param document: Document实例对象
  38.      :param names:多个文件的文件名
  39.      :return:
  40.      “””
  41.      print(“批量生成文件开始”)
  42.      for name in names:
  43.          document.save(os.path.join(“tmp”, name + ‘.docx’))
  44.      print(“批量生成文件结束”)
  45.  
  46.  
  47. def rename_file(path):
  48.      “””
  49.      将path路径下的文件都重命名
  50.      规则:在原文件名后面加上 —> “及答案.” 字段
  51.      :param path:
  52.      :return:
  53.      “””
  54.      print(“重命名开始”)
  55.      files = os.listdir(path)
  56.      for file in files:
  57.          name, doc_type = file.split(“.”)
  58.          new_name = os.path.join(path, name + “及答案.” + doc_type)
  59.          file_tmp = os.path.join(path, file)
  60.          # 语法:
  61.          # os.rename(src, dst) :用于命名文件或目录
  62.          # src:需要修改的文件或目录名。
  63.          # dst:修改后的文件或目录名。
  64.          os.rename(file_tmp, new_name)
  65.      print(“重命名结束”)
  66.  
  67.  
  68. def del_files(path):
  69.      “””
  70.      删除指定路径下的文件
  71.      :param path:
  72.      :return:
  73.      “””
  74.      print(“删除开始”)
  75.      files = os.listdir(path)
  76.      for file in files:
  77.          os.remove(os.path.join(path, file))
  78.      print(“删除成功”)
  79.  
  80.  
  81. if __name__ == ‘__main__’:
  82.      path = r“C:\Users\wz\Desktop\保安员考试试卷”
  83.      document = Document()
  84.      pre_fix = “保安员考试”
  85.      # 生成多个文件名
  86.      # name_all = gen_names(pre_fix, 10)
  87.      #创建多个文件
  88.      # new_many_file(document, name_all)
  89.      # 重命名文件
  90.      # rename_file(“tmp”)
  91.      # 删除文件
  92.      del_files(“tmp”)

以上就是Python实现批量生成,重命名和删除word文件的详细内容,更多关于Python word的资料请关注我们其它相关文章!

标签

发表评论