2017年4月20日 星期四

Use Python with PyAutoGUI to Name a New File Automatically

Use Python with PyAutoGUI to Name a New File Automatically

Use Python with PyAutoGUI to Name a New File Automatically


In this Python program, I use PyAutoGUI to name a new file automatically.

import pyautogui
import time
import pyperclip
pyautogui.PAUSE = 0.5
pyautogui.FAILSAFE = True

def get_date_stamp():
    date_stamp = time.strftime("%Y-%m-%d")
    return date_stamp

def get_old_file_name(file_number):
    old_file_x = 265
    y = 70 + (file_number - 1) * 155
    # print('(old_file_x, y) = ('+ str(old_file_x) + ', ' + str(y) +')')
    pyautogui.moveTo(old_file_x, y)
    pyautogui.click()
    time.sleep(0.5)
    pyautogui.moveRel(0, 50)
    time.sleep(0.5)
    pyautogui.click()
    time.sleep(0.5)
    pyautogui.rightClick()
    time.sleep(0.5)
    pyautogui.press('down')
    pyautogui.press('down')
    pyautogui.press('down')
    pyautogui.press('c') # copy old file name to clip board
    old_file_name = pyperclip.paste() # read old file name from clipboard
    print('Old file name: ' + old_file_name)
    return old_file_name

def get_new_file_name(old_file_name):  
    old_date_stamp = old_file_name[0:10]
    # print('Old date stamp: ' + old_date_stamp)
 
    new_date_stamp = get_date_stamp()
    # print('New date stamp: ' + new_date_stamp)
 
    new_file_name = old_file_name.replace(old_date_stamp, new_date_stamp)
    print('New file name: ' + new_file_name)
 
    return new_file_name

def name_file(file_number, new_file_name):
    new_file_x = 160
    y = 70 + (file_number - 1) * 155      
    print('(new_file_x, y) = ('+ str(new_file_x) + ', ' + str(y) +')')
 
    pyperclip.copy(new_file_name) # copy new file name to clipboard
    pyautogui.moveTo(new_file_x, y)
    pyautogui.click()
    time.sleep(0.5)
    pyautogui.moveRel(0, 50)
    pyautogui.click()
    time.sleep(0.5)
 
    pyautogui.rightClick()
    time.sleep(0.5)
    pyautogui.press('down')
    pyautogui.press('down')
    pyautogui.press('down')
    pyautogui.press('down')
    pyautogui.press('p') # paste new file name from clip board
    time.sleep(0.5)
    pyautogui.press('enter')

# Name new file
def name_new_file(file_number):
    old_file_name = get_old_file_name(file_number)
    new_file_name = get_new_file_name(old_file_name)
    name_file(file_number, new_file_name)

name_new_file(1)

台灣地震預測研究所 所長
林湧森(Dyson Lin)
2017-04-20 13:50 UTC+8

沒有留言:

張貼留言