Python自动化创建照片文件夹:根据Excel时间和姓名生成子文件夹
import\u0020os\nimport\u0020time\nimport\u0020psutil\nfrom\u0020selenium.webdriver.chrome.options\u0020import\u0020Options\nfrom\u0020selenium\u0020import\u0020webdriver\nfrom\u0020selenium.webdriver.common.by\u0020import\u0020By\nimport\u0020openpyxl\n\n#\u0020检查是否已经存在浏览器实例,如果存在则不再创建\nfor\u0020proc\u0020in\u0020psutil.process_iter():\n\u0020\u0020try:\n\u0020\u0020\u0020\u0020if\u0020"chrome"\u0020in\u0020proc.name()\u0020and\u0020"--remote-debugging-port=9224"\u0020in\u0020proc.cmdline():\n\u0020\u0020\u0020\u0020\u0020\u0020options\u0020=\u0020Options()\n\u0020\u0020\u0020\u0020\u0020\u0020options.add_experimental_option("debuggerAddress",\u0020"127.0.0.1:9224")\n\u0020\u0020\u0020\u0020\u0020\u0020break\n\u0020\u0020except:\n\u0020\u0020\u0020\u0020pass\nelse:\n\u0020\u0020#\u0020创建浏览器实例\n\u0020\u0020os.system(r'start\u0020chrome\u0020--remote-debugging-port=9224\u0020--user-data-dir="D:\u8bc4\u9605\u7528"')\n\u0020\u0020options\u0020=\u0020Options()\n\u0020\u0020options.add_experimental_option("debuggerAddress",\u0020"127.0.0.1:9224")\n\n#\u0020在已有的浏览器实例中查找标签页\ndriver\u0020=\u0020webdriver.Chrome(options=options)\ntabs\u0020=\u0020driver.window_handles\nfor\u0020tab\u0020in\u0020tabs:\n\u0020\u0020driver.switch_to.window(tab)\n\u0020\u0020if\u0020driver.title\u0020==\u0020"考后核验":\n\u0020\u0020\u0020\u0020print("登陆成功")\n\u0020\u0020\u0020\u0020break\n\n#\u0020打开Excel表格\nwb\u0020=\u0020openpyxl.load_workbook(r"C:\Users\Administrator\Desktop\考后核验.xlsx")\nsheet\u0020=\u0020wb.active\n\n#\u0020创建照片文件夹\nif\u0020not\u0020os.path.exists(r"C:\Users\Administrator\Desktop\照片"):\n\u0020\u0020os.mkdir(r"C:\Users\Administrator\Desktop\照片")\n\n#\u0020遍历时间单元K列\nfor\u0020row\u0020in\u0020sheet.iter_rows(min_row=2,\u0020max_row=sheet.max_row,\u0020min_col=11,\u0020max_col=11):\n\u0020\u0020for\u0020cell\u0020in\u0020row:\n\u0020\u0020\u0020\u0020#\u0020获取时间单元内的数据\n\u0020\u0020\u0020\u0020data\u0020=\u0020cell.value\n\u0020\u0020\u0020\u0020if\u0020data:\n\u0020\u0020\u0020\u0020\u0020\u0020#\u0020创建子文件夹\n\u0020\u0020\u0020\u0020\u0020\u0020folder_name\u0020=\u0020time.strftime("%Y-%m-%d\u0020%H-%M-%S",\u0020time.strptime(data,\u0020"%Y-%m-%d\u0020%H:%M:%S"))\n\u0020\u0020\u0020\u0020\u0020\u0020folder_path\u0020=\u0020os.path.join(r"C:\Users\Administrator\Desktop\照片",\u0020folder_name)\n\u0020\u0020\u0020\u0020\u0020\u0020if\u0020not\u0020os.path.exists(folder_path):\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020os.mkdir(folder_path)\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020#\u0020遍历表格H列\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020for\u0020h_row\u0020in\u0020sheet.iter_rows(min_row=2,\u0020max_row=sheet.max_row,\u0020min_col=8,\u0020max_col=8):\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020for\u0020h_cell\u0020in\u0020h_row:\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020#\u0020获取H列数据\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020h_data\u0020=\u0020h_cell.value\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020if\u0020h_data:\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020#\u0020创建子文件夹\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020sub_folder_path\u0020=\u0020os.path.join(folder_path,\u0020h_data)\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020if\u0020not\u0020os.path.exists(sub_folder_path):\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020os.mkdir(sub_folder_path)
原文地址: https://www.cveoy.top/t/topic/pH5c 著作权归作者所有。请勿转载和采集!