2017年4月20日 星期四

Use Python with PyAutoGUI to Edit a Quake Forecast Automatically

Use Python with PyAutoGUI to Edit a Quake Forecast Automatically

Use Python with PyAutoGUI to Edit a Quake Forecast Automatically


In this Python program, I use PyAutoGUI to edit a quake forecast automatically.

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

def get_time_stamp():
    time_stamp = time.strftime("%Y-%m-%d")
    time_stamp += ' '
    time_stamp += time.strftime("%H:%M")
    time_stamp += ' UTC+8'
    return time_stamp

quake_forecast_title = {
    'Chinese': '地震預報:',
    'Japanese': '地震予報:',
    'English': 'Quake Forecast: '
}

author = {
    'Chinese': '台灣地震預測研究所 所長\n林湧森',
    'Japanese': '台湾地震予測研究所 所長\n林湧森',
    'English': 'Dyson Lin\nFounder & CEO, Taiwan Quake Forecast Institute'}

Within = {
    'Chinese': '',
    'Japanese': '',
    'English': 'Within '}

Days = {
    'Chinese': '日以內,',
    'Japanese': '日以內に、',
    'English': ' days, '}

Red = {
    'Chinese': '紅:',
    'Japanese': '赤:',
    'English': 'Red: '}

Orange = {
    'Chinese': '橙:',
    'Japanese': 'オレンジ:',
    'English': 'Orange: '}

Yellow = {
    'Chinese': '黃:',
    'Japanese': '黃:',
    'English': 'Yellow: '}

station = {}

station['Taoyuan'] = {
    'Chinese': '桃園站',
    'Japanese': '桃園局',
    'English': 'Taoyuan Station '}

station['Hsinchu Green Light'] = {
    'Chinese': '新竹綠光站',
    'Japanese': '新竹綠光局',
    'English': 'Hsinchu Green Light Station '}

station['Yilan'] = {
    'Chinese': '宜蘭站',
    'Japanese': '宜蘭局',
    'English': 'Yilan Station '}

station['Taichung Tantzu'] = {
    'Chinese': '台中潭子站',
    'Japanese': '台中潭子局',
    'English': 'Taichung Tantzu Station '}

station['Tainan Xinsheng'] = {
    'Chinese': '台南新生站',
    'Japanese': '台南新生局',
    'English': 'Tainan Xinsheng Station '}

station['Tainan Joy & Luck'] = {
    'Chinese': '台南喜福站',
    'Japanese': '台南喜福局',
    'English': 'Tainan Joy & Luck Station '}

station['Kaohsiung'] = {
    'Chinese': '高雄站',
    'Japanese': '高雄局',
    'English': 'Kaohsiung Station '}

station['Tainan Hot Heart'] = {
    'Chinese': '台南熱心站',
    'Japanese': '台南熱心局',
    'English': 'Tainan Hot Heart Station '}

station['Durban, South Africa'] = {
    'Chinese': '南非德本站',
    'Japanese': '南アフリカ ダーバン局',
    'English': 'Durban, South Africa Station '}

station['Nantou Caotun'] = {
    'Chinese': '南投草屯站',
    'Japanese': '南投草屯局',
    'English': 'Nantou Caotun Station '}

station['Faenza, Italy'] = {
    'Chinese': '義大利法恩扎站',
    'Japanese': 'イタリア ファエンツァ局',
    'English': 'Faenza, Italy Station '}

station_name = station['Durban, South Africa']

quake_forecast = [
    {'color': Red,
    'time': 2,
    'location': {
        'Chinese': '東台灣或南日本,',
        'Japanese': '東台湾または南日本、',
        'English': 'East Taiwan or South Japan, '},
    'magnitude': 'M6+ ~ M7+'},
    {'color': Orange,
    'time': 10,
    'location': {
        'Chinese': '東台灣或南日本,',
        'Japanese': '東台湾または南日本、',
        'English': 'East Taiwan or South Japan, '},
    'magnitude': 'M6+ ~ M7+'}]

time_stamp = get_time_stamp()
languages = ['Chinese', 'Japanese', 'English']

blog_title = {}
for language in languages:
    blog_title[language] = station_name[language]
    blog_title[language] += quake_forecast_title[language]
    quake_number = 0
    for quake in quake_forecast:
        quake_number += 1
        blog_title[language] += '('
        blog_title[language] += str(quake_number)
        blog_title[language] += ')'
        blog_title[language] += quake['color'][language]
        blog_title[language] += Within[language]
        blog_title[language] += str(quake['time'])
        blog_title[language] += Days[language]
        blog_title[language] += quake['location'][language]
        blog_title[language] += quake['magnitude']
        blog_title[language] += ' '
blog_title_str = (time_stamp + ' ')
blog_title_str += (blog_title['Chinese'])
blog_title_str += (blog_title['Japanese'])
blog_title_str += blog_title['English']
blog_title_str = blog_title_str.strip() # remove extra white space
print(blog_title_str)
pyperclip.copy(blog_title_str) # copy to clipboard

blog_text = {}
for language in languages:
    blog_text[language] = station_name[language]
    blog_text[language] += quake_forecast_title[language] + '\n'
    quake_number = 0
    for quake in quake_forecast:
        quake_number += 1
        blog_text[language] += '('
        blog_text[language] += str(quake_number)
        blog_text[language] += ')'
        blog_text[language] += quake['color'][language]
        blog_text[language] += Within[language]
        blog_text[language] += str(quake['time'])
        blog_text[language] += Days[language]
        blog_text[language] += quake['location'][language]
        blog_text[language] += quake['magnitude']
        blog_text[language] += '\n'
    blog_text[language] += '\n'    
    blog_text[language] += author[language]
    blog_text[language] += '\n'
    blog_text[language] += time_stamp
    blog_text[language] += '\n\n\n'

blog_text_str = ''        
for language in languages:
    blog_text_str += blog_text[language]
blog_text_str = blog_text_str.strip() # remove extra white space
print(blog_text_str)
pyperclip.copy(blog_text_str) # copy to clipboard

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

Use Python with PyAutoGUI to Duplicate a File and Rename it Automatically

Use Python with PyAutoGUI to Duplicate a File and Rename it Automatically

Use Python with PyAutoGUI to Duplicate a File and Rename it Automatically


In this Python program, I use PyAutoGUI to duplicate a file and rename it automatically.

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

# Duplicate file
def duplicate_file():
    pyautogui.rightClick()
    time.sleep(1)
    pyautogui.press('c')
   
    pyautogui.moveRel(100, 0)
    pyautogui.rightClick()
    pyautogui.press('p')
    time.sleep(1)

# Ramane file to file name - Analysis
def rename_file():
    pyautogui.click()
    time.sleep(1)
    pyautogui.moveRel(0, 60)
    pyautogui.click()
    time.sleep(1)
    pyautogui.press('end')
    for i in range(0, 4):
        pyautogui.press('left')
    for i in range(0, 2):
        pyautogui.press('backspace')
    pyautogui.typewrite('Analysis')
    pyautogui.press('enter')

# Open file with Painter
def open_file():
    pyautogui.doubleClick()
    time.sleep(2)
    pyautogui.moveTo(445, 65)
    pyautogui.click() # select Rectangle tool

# Duplicate file, ramane it and open it with Painter
def analyze_file(file_number):
    x = 160
    y = 70 + (file_number - 1) * 155
       
    print('(x, y) = ('+ str(x) + ', ' + str(y) +')')
    pyautogui.moveTo(x, y)
    duplicate_file()
    rename_file()
    open_file()

analyze_file(1)

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

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

2017年4月17日 星期一

Python入門:print()的用法:登鸛雀樓

登鸛雀樓

print()用來在螢幕上印出字串。

print()的用法:
    print(字串):印出字串,並換行。
    print():印出空白行。

字串放在一對單引號(')或雙引號之間(")。
例如:
    'Hello'
    "你好"

以下Python程式,將在螢幕上印出登鸛雀樓這首詩:
    print('登鸛雀樓 ')
    print('王之渙')
    print()
    print('白日依山盡,')
    print('黃河入海流。')
    print('欲窮千里目,')
    print('更上一層樓。')

台灣地震預測研究所 所長
林湧森
2017-04-17 11:40 UTC+8

2017年4月11日 星期二

第一個Python程式:使用Jupyter Notebook(iPython Notebook)

Jupyter Notebook (iPython Notebook)

在安裝好Anaconda之後,我們就可以透過Jupyter Notebook(iPython Notebook)的圖形介面,輕鬆使用Python。

以下影片,示範如何使用Jupyter Notebook(iPython Notebook):


How to use Jupyter Notebook(iPython Notebook)


Jupyter Notebook(iPython Notebook)會把我們輸入的每一段程式碼編號。

在In [ ]: 提示符號的右邊輸入:
    print('Hello, quakes!')

輸入完畢之後,以滑鼠點擊上方工具列的向右箭頭,或是按鍵盤上的Shift-Enter(按住Shift不放,然後按Enter),程式碼就開始執行,在底下輸出:
    Hello, quakes!

這就完成了我們的第一個Python程式。

台灣地震預測研究所 所長
林湧森
2017-04-11 08:52 UTC+8

學習Python的第一步:安裝Anaconda

Download Anaconda 4.3.1 For Windows

現在最受歡迎的程式語言是Python。
Python能拿來做各種用途。
我打算用Python來協助我預測地震。

Python的套件很多,可透過pip自行安裝。
但是對初學者而言,最簡單的方法,就是安裝Anaconda。
Anaconda會安裝Python以及各種常用的套件。

請到Anaconda官網,下載適合你作業系統的版本安裝:https://www.continuum.io/downloads
我建議安裝Python 3.6。

安裝過程可能很久,請耐心等待。

台灣地震預測研究所 所長
林湧森
2017-04-11 08:12 UTC+8

Python入門部落格開張囉

這是我的新部落格。
主題是Python入門。
將會發表一系列文章。
目標是以Python預測地震。

請拭目以待。
謝謝!

台灣地震預測研究所 所長
林湧森
2017-04-11 07:49 UTC+8