提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
def copy_files(original_file, num_copies):
# 获取原始文件的目录和文件名(不包括扩展名)
directory, file_base = os.path.split(original_file)
file_name, file_ext = os.path.splitext(file_base)
for i in range(1, num_copies + 1):
# 构建新文件名
new_file_name = f"{file_name}_copy{i}{file_ext}"
new_file_path = os.path.join(directory, new_file_name)
# 复制文件
shutil.copy2(original_file, new_file_path)
print(f"Copied to {new_file_path}")
if __name__ == "__main__":
# 指定要复制的文件路径
original_file_path = "E:\要交的赶紧搞\过程性评价表\G201-XXX.docx"
number_of_copies = 304
def rename_from_excel():
"""根据excel表格的内容批量修改文件名"""
# excel表格所在的位置
excel_path = r"E:\要交的赶紧搞\11过程性评价成绩--信.xlsx"
# 读取excel表:批量处理测试.xlsx
xlsx1 = xlrd.open_workbook(excel_path)
# 读取表格里第一个sheet(工作簿)
sheet = xlsx1.sheet_by_index(0)
# 获取表格第二列数据 -- 班级
class_list = list(sheet.col_values(1))
# 获取表格第四列数据 -- 姓名
name_list = list(sheet.col_values(3))
print(class_list)
print(name_list)
# 获取该文件夹下所有的文件(包括文件夹)
original_file_path = "E:\要交的赶紧搞\过程性评价表"
i = 0
file_names = os.listdir(original_file_path)
for file_name in file_names:
original_rename = os.path.join(original_file_path, file_name) # 获取所有文件的路径
directory, file_base = os.path.split(original_file_path)
file_name, file_ext = os.path.splitext(file_base)
print('original_rename',original_rename)
print('directory',directory)
print('file_base',file_base)
print('file_name',file_name)
print('file_ext',file_ext)
file_name = class_list[i] + '-' + name_list[i] + '-信息技术过程性评价成绩第3学期.docx'
new_file_path = os.path.join(original_file_path, file_name+file_ext)
print('new_file_path',new_file_path)
os.rename(original_rename, new_file_path)
operator_docx(new_file_path, class_list[i], name_list[i])
i = i + 1
def operator_docx(file_path,class_name, all_name):
"""自动化操作--docx"""
# 打开一个现有的Document对象
doc = Document(f'{file_path}')
# paras = list(doc.paragraphs)
# print('==============')
# 遍历所有段落并打印其内容
# for para in doc.paragraphs:
# print(para.text)
# print(doc.paragraphs[3].text)
doc.paragraphs[3].text = f'学校:XXX中学 班级:{class_name} 学生姓名:{all_name} 第 3学期/模块'
doc.save(f'{file_path}')
# print(type(para))
# print(type(paras))
# print('===========================')
# 遍历所有表格并打印其内容 -- 你可以去看看里面的内容
# for table in doc.tables:
# for row in table.rows:
# for cell in row.cells:
# print(cell.text, end='\t')
# print()
import os
import shutil
import xlrd
from docx import Document
import pandas
def copy_files(original_file, num_copies):
# 获取原始文件的目录和文件名(不包括扩展名)
directory, file_base = os.path.split(original_file)
file_name, file_ext = os.path.splitext(file_base)
for i in range(1, num_copies + 1):
# 构建新文件名
new_file_name = f"{file_name}_copy{i}{file_ext}"
new_file_path = os.path.join(directory, new_file_name)
# 复制文件
shutil.copy2(original_file, new_file_path)
print(f"Copied to {new_file_path}")
def rename_from_excel():
"""根据excel表格的内容批量修改文件名"""
# excel表格所在的位置
excel_path = r"E:\要交的赶紧搞\11过程性评价成绩--信.xlsx"
# 读取excel表:批量处理测试.xlsx
xlsx1 = xlrd.open_workbook(excel_path)
# 读取表格里第一个sheet(工作簿)
sheet = xlsx1.sheet_by_index(0)
# 获取表格第二列数据 -- 班级
class_list = list(sheet.col_values(1))
# 获取表格第四列数据 -- 姓名
name_list = list(sheet.col_values(3))
print(class_list)
print(name_list)
# 获取该文件夹下所有的文件(包括文件夹)
original_file_path = "E:\要交的赶紧搞\过程性评价表"
i = 0
file_names = os.listdir(original_file_path)
for file_name in file_names:
original_rename = os.path.join(original_file_path, file_name) # 获取所有文件的路径
directory, file_base = os.path.split(original_file_path)
file_name, file_ext = os.path.splitext(file_base)
print('original_rename',original_rename)
print('directory',directory)
print('file_base',file_base)
print('file_name',file_name)
print('file_ext',file_ext)
file_name = class_list[i] + '-' + name_list[i] + '-信息技术过程性评价成绩第3学期.docx'
new_file_path = os.path.join(original_file_path, file_name+file_ext)
print('new_file_path',new_file_path)
os.rename(original_rename, new_file_path)
operator_docx(new_file_path, class_list[i], name_list[i])
i = i + 1
def operator_docx(file_path,class_name, all_name):
"""自动化操作--docx"""
# 打开一个现有的Document对象
doc = Document(f'{file_path}')
# paras = list(doc.paragraphs)
# print('==============')
# 遍历所有段落并打印其内容
# for para in doc.paragraphs:
# print(para.text)
# print(doc.paragraphs[3].text)
doc.paragraphs[3].text = f'学校:深圳市红岭教育集团大鹏华侨中学 班级:{class_name} 学生姓名:{all_name} 第 3学期/模块'
doc.save(f'{file_path}')
# print(type(para))
# print(type(paras))
# print('===========================')
# 遍历所有表格并打印其内容 -- 你可以去看看里面的内容
# for table in doc.tables:
# for row in table.rows:
# for cell in row.cells:
# print(cell.text, end='\t')
# print()
if __name__ == "__main__":
# 指定要复制的文件路径
original_file_path = "E:\要交的赶紧搞\过程性评价表\G201-蔡承君.docx"
number_of_copies = 304
# 1.批量复制文件
copy_files(original_file_path, number_of_copies)
# 2.用excel的行名得到想要的文件名 --> 自动化操作函数operator_docx
rename_from_excel()
from docx import Document
# 打开一个现有的Document对象
doc = Document('demo.docx')
# 遍历所有段落并打印其内容
for para in doc.paragraphs:
print(para.text)
# 遍历所有表格并打印其内容
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
print(cell.text, end='\t')
print()
# 假设我们要修改第2个段落的内容
target_paragraph_index = 1 # 注意索引从0开始,所以第2个段落的索引是1
# 确保文档中有足够的段落
if len(doc.paragraphs) > target_paragraph_index:
# 修改第2个段落的内容
doc.paragraphs[target_paragraph_index].text = '这是新的段落内容。'
# 保存修改后的文档
doc.save('modified_by_index_example.docx')
# excel表格所在的位置
excel_path = r"E:\要交的赶紧搞\11过程性评价成绩--信.xlsx"
# 读取excel表:批量处理测试.xlsx
xlsx1 = xlrd.open_workbook(excel_path)
# 读取表格里第一个sheet(工作簿)
sheet = xlsx1.sheet_by_index(0)
# 获取表格第二列数据 -- 班级
class_list = list(sheet.col_values(1))
# 获取表格第四列数据 -- 姓名
name_list = list(sheet.col_values(3))
print(class_list)
print(name_list)
# 获取该文件夹下所有的文件(包括文件夹)
original_file_path = "E:\要交的赶紧搞\过程性评价表"