🐾 一些实用的小脚本
最近用GPT编写了一些好用的脚本,可以很大程度上提升工作效率,分享给大家:
❤️ 信息提取及编辑
这个代码可以从一大堆信息的文件夹中提取出来指定的文件,并且重新编号放到新文件夹中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 import osimport shutilimport pandas as pdroot_dir = r'E:\文件\研究生\课题组\目诊标注\目-截止0718' target_base_dir = r'E:\文件\研究生\课题组\目诊标注\report' target_image_name = 'report.pdf' image_info = [] for subdir, _, files in os.walk(root_dir): if target_image_name in files: current_dir = os.path.basename(subdir) image_path = os.path.join(subdir, target_image_name) target_dir = os.path.join(target_base_dir, target_image_name[:-4 ]) if not os.path.exists(target_dir): os.makedirs(target_dir) new_image_index = len (image_info) + 1 new_image_name = f'{new_image_index} .pdf' new_image_path = os.path.join(target_dir, new_image_name) shutil.copy(image_path, new_image_path) image_info.append((current_dir, new_image_index)) excel_path = os.path.join(target_base_dir, f'{target_image_name[:-4 ]} .xlsx' ) df = pd.DataFrame(image_info, columns=['上一级目录' , '重命名后的编号' ]) df.to_excel(excel_path, index=False ) print ("任务完成!" )
这个代码可以从指定文件中提取相应的信息并且整理到一个表格里,唯一需要的就是我们需要将表格不同列的信息稍作整理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import osimport reimport pandas as pdfrom PyPDF2 import PdfReaderfolder_path = r"E:\文件\研究生\课题组\目诊标注\report\report" output_excel = os.path.join(folder_path, "extracted_data.xlsx" ) patterns = { "姓名" : r"姓名:\s*([^\s]+)" , "身份证" : r"身份证:\s*([\dXx]+)" , "病历号" : r"病历号:\s*([^\s]+)" , "手机" : r"手机:\s*([\d-]+)" } data = [] pdf_files = sorted ( [f for f in os.listdir(folder_path) if f.endswith(".pdf" )], key=lambda x: int (re.findall(r'\d+' , x)[0 ]) ) for file_name in pdf_files: pdf_path = os.path.join(folder_path, file_name) try : reader = PdfReader(pdf_path) text = "" for page in reader.pages: text += page.extract_text() + "\n" extracted_data = [file_name] for key, pattern in patterns.items(): match = re.search(pattern, text, re.MULTILINE) value = match .group(1 ) if match else "" extracted_data.append(value) data.append(extracted_data) except Exception as e: print (f"Error processing file {file_name} : {e} " ) columns = ["文件名" , "姓名" , "身份证" , "病历号" , "手机" ] df = pd.DataFrame(data, columns=columns) df.to_excel(output_excel, index=False ) print (f"提取完成,结果已保存到 {output_excel} " )
上面的代码运行的时候别忘了修改需要操作的文件路径,建议自己先拿一个小文件夹试验一下,可以了再应用到需要操作的所有文件里。
❤️模拟器自动点击
使用mumu模拟器打开拼多多或小红书时,可能会出现回车发不了消息的情况,我们可以设置AutoHotkey自动脚本来实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #Persistent ; 让脚本一直运行 Enter:: NumpadEnter:: IfWinExist, ahk_class Qt5156QWindowIcon { if (WinActive("ahk_class Qt5156QWindowIcon" )) { MouseMove, 1798 , 994 MouseClick, left } } Else IfWinExist, ahk_class AnotherWindowClass { if (WinActive("ahk_class AnotherWindowClass" )) { MouseMove, 1796 , 989 MouseClick, left } } Return
AutoHotkey 是一种用于自动化任务和创建快捷键的编程语言,通常用于模拟键盘输入、鼠标操作等。