3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

業務自動化の達人になろう:AutoHotkeyとPythonを使った5つの実践例

Posted at

はじめに

日々の繰り返し作業に疲れていませんか?本記事では、AutoHotkey(AHK)とPythonという2つの強力なツールを使って、単調な作業を簡単に自動化する方法を紹介します。初心者にも扱いやすい具体例を5つ挙げながら、業務効率化の第一歩を踏み出しましょう。

AutoHotkeyとは

AutoHotkeyは、Windowsで動作する強力なスクリプト言語です。キーボードやマウスの操作を自動化したり、アプリケーションを制御したりすることができます。

AutoHotkeyのインストール方法

  1. AutoHotkey公式サイトにアクセス
  2. ダウンロードページからインストーラーをダウンロード
  3. インストーラーを実行し、指示に従ってインストール

Pythonとは

Pythonは読みやすく書きやすい汎用プログラミング言語で、業務自動化にも適しています。

Pythonのセットアップ

  1. Python公式サイトからインストーラーをダウンロード
  2. インストーラーを実行し、指示に従ってインストール
  3. コマンドプロンプトで python --version を実行し、インストールを確認

自動化の具体例

1. タイムカードの自動打刻

AutoHotkeyバージョン

; Chromeでタイムカードシステムを開く
Run, chrome.exe "https://example-timetracker.com/login"
WinWaitActive, ahk_class Chrome_WidgetWin_1

; ログイン情報を入力
Send, your_username{Tab}
Send, your_password{Enter}

; 打刻ボタンをクリック
Sleep, 3000
Click, 100, 200

Pythonバージョン(Seleniumを使用)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ChromeDriverのパスを指定してブラウザを起動
driver_path = 'path/to/chromedriver'
driver = webdriver.Chrome(driver_path)

# タイムカードシステムにアクセス
driver.get('https://example-timetracker.com/login')

# ログイン操作
username = driver.find_element(By.NAME, 'username')
password = driver.find_element(By.NAME, 'password')
login_button = driver.find_element(By.NAME, 'login')

username.send_keys('your_username')
password.send_keys('your_password')
login_button.click()

# 打刻ボタンをクリック
try:
    button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'punch_button'))
    )
    button.click()
except Exception as e:
    print(f"Error: {e}")
finally:
    driver.quit()

2. Zoom会議への自動参加

AutoHotkeyバージョン

; Zoomを起動
Run, "C:\path\to\Zoom\bin\Zoom.exe"
WinWaitActive, ahk_class ZPPTMainFrmWndClass

; 会議IDとパスコードを入力
Send, 1234567890{Enter}
Sleep, 1000
Send, your_meeting_password{Enter}

Pythonバージョン(pyautoguiを使用)

import pyautogui
import time
import subprocess

# Zoomを起動
subprocess.Popen("C:\\path\\to\\Zoom\\bin\\Zoom.exe")
time.sleep(5)  # Zoomが起動するまで待機

# 会議IDとパスコードを入力
pyautogui.write('1234567890')
pyautogui.press('enter')
time.sleep(2)
pyautogui.write('your_meeting_password')
pyautogui.press('enter')

3. 定期レポートの自動生成と送信

AutoHotkeyバージョン

; Excelでレポートを生成
Run, excel.exe "C:\path\to\your_report.xlsx"
WinWaitActive, ahk_class XLMAIN
Send, ^+r ; レポート生成マクロを実行
Sleep, 5000
Send, ^s ; 保存

; Outlookでメール送信
Run, outlook.exe
WinWaitActive, ahk_class rctrl_renwnd32
Send, ^n
Send, recipient@example.com{Tab}
Send, Weekly Report{Tab}{Tab}
Send, Please find the attached report.{Tab}{Enter}
Send, ^v ; レポートを添付
Send, !s ; 送信

Pythonバージョン(openpyxlとwin32com.clientを使用)

import openpyxl
import win32com.client as win32

# Excelでレポートを生成
wb = openpyxl.load_workbook('C:\\path\\to\\your_report.xlsx')
sheet = wb.active
# レポート生成のロジックをここに記述
wb.save('C:\\path\\to\\generated_report.xlsx')

# Outlookでメール送信
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'recipient@example.com'
mail.Subject = 'Weekly Report'
mail.Body = 'Please find the attached report.'
mail.Attachments.Add('C:\\path\\to\\generated_report.xlsx')
mail.Send()

4. 定期的なファイルバックアップ

AutoHotkeyバージョン

; バックアップスクリプトを実行
Run, "C:\path\to\backup_script.bat"
Sleep, 10000

; 完了通知
MsgBox, Backup completed successfully!

Pythonバージョン

import shutil
import datetime

# バックアップ元とバックアップ先のパス
source = "C:\\path\\to\\source"
destination = "D:\\path\\to\\backup"

# 現在の日時をフォルダ名に使用
now = datetime.datetime.now()
backup_folder = f"{destination}\\backup_{now.strftime('%Y%m%d_%H%M%S')}"

# バックアップの実行
shutil.copytree(source, backup_folder)

print("Backup completed successfully!")

5. 日次データ入力

AutoHotkeyバージョン

; Excelファイルを開く
Run, excel.exe "C:\path\to\your_data_entry_file.xlsx"
WinWaitActive, ahk_class XLMAIN

; データ入力
Send, ^g
Send, A1{Enter}
Send, Today's Date: %A_Now%{Enter}
Send, ^s

Pythonバージョン(openpyxlを使用)

from openpyxl import load_workbook
from datetime import datetime

# Excelファイルを開く
wb = load_workbook('C:\\path\\to\\your_data_entry_file.xlsx')
sheet = wb.active

# データ入力
sheet['A1'] = f"Today's Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"

# 保存
wb.save('C:\\path\\to\\your_data_entry_file.xlsx')

まとめ

AutoHotkeyとPythonは、それぞれ異なる特徴を持つ強力な自動化ツールです。

  • AutoHotkeyは、Windowsに特化した操作の自動化に優れています。
  • Pythonは、より複雑なロジックや、多様なライブラリを活用した自動化に適しています。

これらのツールを状況に応じて使い分けることで、効率的な業務自動化が実現できます。コードも比較的初心者でも分かるように心がけました!
この記事で紹介した例を参考に、自分の業務に合わせたスクリプトを作成してみてください。作業効率が大幅に向上するはずです!

参考リンク


3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?