您的位置:首页 > 游戏 > 游戏 > 【办公类-50-01】20240620自主游戏观察记录表19周内容打乱

【办公类-50-01】20240620自主游戏观察记录表19周内容打乱

2024/10/5 17:22:36 来源:https://blog.csdn.net/reasonsummer/article/details/139886467  浏览:    关键词:【办公类-50-01】20240620自主游戏观察记录表19周内容打乱

背景需求:

又到了期末,各种班级资料需要提交。

有一份自主游戏观察记录需要写19周(每周2次)的观察记录,并根据参考书填写一级、三级、五级的评价指标。

去年中六班的时候,我很认真的手写了21周的户外游戏活动内容,主动成为2个需要提交文本资料的班级。

今年组长选了中二和中五提交打印资料。因此中四班的游戏观察资料可以不那么“优质”,所以本学期我想“偷个懒”,再次使用去年的内容做电子稿提交。

考虑到这套资料也给过其他班级,我担心会出现重复,所以有两个调整的思路:

1、将每个周次的内容进行替换,如原来第1周的内容放到第15周,第13周内容调到第2周。

2、虽然内容没有修改,但至少将孩子的名字替换成新的名字(中四班孩子的小名)

设计过程。

1、原素材

2、模板

3、代码思路

(1)读取原素材表格中各个单元格里面的内容,新建一个excel并保存这些内容

 (2)手动更改小名:因为不确定出现哪些名字,需要把表格拉宽,查看里面的名字。这里程序要会暂停。

(3)改完以后,打乱行的顺序,把文字内容重写排列,

(4)把excel每行内容生成一个docx,把所有doc合并在一起

然后程序把所有的docx合并在一个docx内

部分表格手动调整(删除回车符),确保每个表格内容都在一页上。

代码展示:

'''
项目:期末资料:19周游戏观察记录内容周次调换
工具:星火讯飞、阿夏
时间:2024年6月20日
'''import re
import pandas as pd
from docx import Document
import timefor y in range(5):path = r'C:\Users\jg2yXRZ\OneDrive\桌面\游戏观察记录'# 读取1.docx文件doc = Document(path + r"\中(6)班自主游戏观察记录(202302-202306)全部户外.docx")weeks=19 # 一共几周name1='张三'name2='李四'print('---1、提取原来的游戏观察记录内容-----')# 提取所有段落中“第”和“周”之间的内容results = []for paragraph in doc.paragraphs:content = paragraph.textpattern = r"第(.*?)周"result = re.findall(pattern, content)if result:results.extend(result)# 缩进2字符b='    ' table_data1 = []for table in doc.tables:cell_value1 = table.cell(0, 0).text.replace(" ", "")  # 清除缩进lines1 = cell_value1.split('\n')del lines1[0]  # 删除第一行new_lines1 = [b + line for line in lines1]  # 在每行前面加四个空格new_cell_value1 = '\n'.join(new_lines1)table_data1.append(new_cell_value1)print(new_cell_value1)table_data2 = []for table in doc.tables:cell_value2 = table.cell(2, 0).text.replace(" ", "")   # 清除缩进lines2 = cell_value2.split('\n')del lines2[0]  # 删除第一行new_lines2 = [b + line for line in lines2]  # 在每行前面加四个空格new_cell_value2 = '\n'.join(new_lines2)table_data2.append(new_cell_value2)print(new_cell_value2)table_data3 = []for table in doc.tables:cell_value3 = table.cell(0, 1).text.replace(" ", "")   # 清除缩进lines3 = cell_value3.split('\n')del lines3[0]  # 删除第一行new_lines3 = [b + line for line in lines3]  # 在每行前面加四个空格new_cell_value3 = '\n'.join(new_lines3)table_data3.append(new_cell_value3)print(new_cell_value3)table_data4 = []for table in doc.tables:cell_value4 = table.cell(2, 1).text.replace(" ", "")   # 清除缩进lines4 = cell_value4.split('\n')del lines4[0]  # 删除第一行new_lines4 = [b + line for line in lines4]  # 在每行前面加四个空格new_cell_value4 = '\n'.join(new_lines4)table_data4.append(new_cell_value4)print(new_cell_value4)table_data5= []for table in doc.tables:cell_value5 = table.cell(1, 1).text.replace(" ", "")   # 清除缩进print(cell_value5)table_data5.append(cell_value5[3:])# print(new_cell_value5)table_data6= []for table in doc.tables:cell_value6 = table.cell(3, 1).text.replace(" ", "")   # 清除缩进print(cell_value6)table_data6.append(cell_value6[3:])# print(new_cell_value5) df = pd.DataFrame({'Week': results, 'content1': table_data1,'content2': table_data2,'fx1': table_data3,'fx2': table_data4,'T1': table_data5,'T2': table_data6})df.to_excel(path + '\\数据导出改人名.xlsx', index=False)# 手动改人名print("手动改人名,按任意键继续...")input()  # 这里会暂停程序,直到用户输入信息print("程序继续执行...")print('---2、手动改人名,随机抽取行数,重新保存一份----')import pandas as pd# 读取Excel文件data = pd.read_excel(path + '\\数据导出改人名.xlsx')# 获取行数num_rows = len(data)print(num_rows)# 19# 打乱行顺序shuffled_data = data.sample(frac=1).reset_index(drop=True)# 保存打乱顺序后的数据为新的Excel文件shuffled_data.to_excel(path +r'\更换顺序.xlsx', index=False)# 更改周次和姓名import openpyxl# 打开Excel文件workbook = openpyxl.load_workbook(path +r'\更换顺序.xlsx')# 选择工作表worksheet = workbook.active# 在A列第2行开始写入1-20的数字for i in range(2, num_rows+2):worksheet.cell(row=i, column=1).value = i - 1# 在F列写入教室A的名字for i in range(2, num_rows+2):worksheet.cell(row=i, column=6).value = name1# 在G列写入教室A的名字for i in range(2, num_rows+2):worksheet.cell(row=i, column=7).value = name2time.sleep(2)# 保存修改后的Excel文件workbook.save(path+r'\更换顺序.xlsx')print('---3、写入模板生成新的文件--')import osimport pandas as pdfrom docxtpl import DocxTemplatefile_path = path + r'\04合成2'try:os.mkdir(file_path)except:passlist = pd.read_excel(path + '\\更换顺序.xlsx')Week= list["Week"]content1 = list["content1"].str.rstrip()content2 = list["content2"].str.rstrip()fx1 = list["fx1"].str.rstrip()fx2 = list["fx2"].str.rstrip()T1 = list["T1"].str.rstrip()T2 = list["T2"].str.rstrip()num = list.shape[0]for i, (w, c1, c2, f1, f2 ,t1, t2) in enumerate(zip(Week, content1, content2, fx1,fx2,T1, T2)):context = {"Week": w,"content1": c1, "content2": c2,"fx1": f1,"fx2": f2, "T1": t1,"T2": t2,}tpl = DocxTemplate(path + '\\模版游戏观察记录与反思(2024.1).docx')tpl.render(context)time.sleep(4)tpl.save(file_path + fr"\第{i+1:02d}周.docx")print('---4、合并文档--')import osimport shutilfrom docx import Documentdef merge_docx_files(input_folder, output_file):output_doc = Document(r'C:\Users\jg2yXRZ\OneDrive\桌面\游戏观察记录\04合成2\第01周.docx')   files = os.listdir(input_folder)for index, file in enumerate(files):if file.endswith(".docx") and index != 0:input_file = os.path.join(input_folder, file)input_doc = Document(input_file)# output_doc.add_page_break()  # 添加换页符for element in input_doc.element.body:    output_doc.element.body.append(element)time.sleep(2)output_doc.save(output_file)input_folder = file_pathoutput_file = path+fr"\{y:02d}中4班 游戏活动观察记录(202402-202406)全部户外.docx"merge_docx_files(input_folder, output_file)

存在问题:

本代码有一个问题,就是每次提取的文字会有缺失,原因不明,

所以我就生成多份随机排序的文件,从中挑选一个内容相对多的文件,作为提交的电子稿的资料。

运气好,以下这份文字较为齐全(撑满格子)

哎,偷懒就不能保证打乱文字的稳定性,所以要多抽几次博一个成功的概率,或者还是乖乖的自己手写吧!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com