Pythonで実現するオフィス自動化の実用テクニック

ファイル一覧の取得

import os

def scan_directory(path):
    for folder, _, filenames in os.walk(path):
        for name in filenames:
            full_path = os.path.join(folder, name)
            print(full_path)

# 使用例
scan_directory('/path/to/target')

周期的タスクの実行

import schedule
import time

def daily_backup():
    print("バックアップ処理が実行されました")

schedule.every().day.at("02:00").do(daily_backup)
schedule.every().hour.do(lambda: print("ホスト監視中..."))

while True:
    schedule.run_pending()
    time.sleep(30)

Webページからのデータ抽出

import requests
from bs4 import BeautifulSoup

def extract_page_title(url):
    res = requests.get(url, timeout=10)
    soup = BeautifulSoup(res.content, 'html.parser')
    title_tag = soup.find('title')
    return title_tag.get_text(strip=True) if title_tag else "タイトル未取得"

# 使用例
title = extract_page_title('http://example.com')
print(f"ページタイトル: {title}")

メール送信の自動化

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_notification(recipient, subject, body):
    msg = MIMEMultipart()
    msg['From'] = 'sender@company.com'
    msg['To'] = recipient
    msg['Subject'] = subject

    msg.attach(MIMEText(body, 'plain', 'utf-8'))

    with smtplib.SMTP('smtp.company.com', 587) as server:
        server.starttls()
        server.login('sender@company.com', 'secure_password')
        server.send_message(msg)

# 使用例
send_notification('recipient@company.com', '自動通知', 'システム処理が完了しました。')

データの整形と分析

import pandas as pd

def clean_sales_data(raw_data):
    df = pd.DataFrame(raw_data)
    df['date'] = pd.to_datetime(df['date'], errors='coerce')
    df.dropna(subset=['date', 'amount'], inplace=True)
    df['amount'] = pd.to_numeric(df['amount'], errors='coerce')
    return df[df['amount'] > 0]

# 使用例
sales_records = {
    'date': ['2023-01-01', '2023-01-02', 'invalid'],
    'amount': [1200, 850, 'abc']
}
cleaned = clean_sales_data(sales_records)
print(cleaned)

PDFページ数のカウント

import PyPDF2

def get_pdf_page_count(filepath):
    with open(filepath, 'rb') as f:
        pdf_reader = PyPDF2.PdfReader(f)
        return len(pdf_reader.pages)

# 使用例
page_count = get_pdf_page_count('/reports/annual.pdf')
print(f"PDFのページ数: {page_count}")

SQLiteデータベースの操作

import sqlite3

def init_db(db_path):
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS employees (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            department TEXT,
            hire_date DATE
        )
    ''')
    conn.commit()
    conn.close()

def add_employee(db_path, name, dept, hire_date):
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    cursor.execute('INSERT INTO employees (name, department, hire_date) VALUES (?, ?, ?)',
                   (name, dept, hire_date))
    conn.commit()
    conn.close()

# 使用例
init_db('company.db')
add_employee('company.db', '佐藤太郎', '営業部', '2023-04-01')

画像の回転処理

from PIL import Image

def transform_image(input_path, output_path, angle=90):
    with Image.open(input_path) as img:
        rotated = img.rotate(angle, expand=True)
        rotated.save(output_path, quality=95)

# 使用例
transform_image('photo.jpg', 'rotated_photo.jpg', 180)

Webフォームの自動入力

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

def auto_fill_form(url, username, password):
    driver = webdriver.Chrome()
    try:
        driver.get(url)
        wait = WebDriverWait(driver, 10)
        username_field = wait.until(EC.presence_of_element_located((By.NAME, "username")))
        password_field = driver.find_element(By.NAME, "password")
        
        username_field.send_keys(username)
        password_field.send_keys(password)
        driver.find_element(By.ID, "login-btn").click()
    finally:
        driver.quit()

# 使用例(実際のURLと要素IDに置き換えて使用)
# auto_fill_form('https://login.example.com', 'user123', 'pass456')

Word文書の自動生成

from docx import Document

def generate_report(title, content, output_file):
    doc = Document()
    doc.add_heading(title, level=1)
    doc.add_paragraph(content)
    doc.add_paragraph(f"作成日: {pd.Timestamp.now().strftime('%Y年%m月%d日')}")
    doc.save(output_file)

# 使用例
generate_report('月次報告書', '本月の売上は前月比12%増加しました。', 'report.docx')

受信メールの自動取得

import time
import schedule
from imapclient import IMAPClient
import email

def check_new_emails():
    server = IMAPClient('imap.company.com', use_uid=True)
    server.login('user@company.com', 'app_password')
    server.select_folder('INBOX')

    messages = server.search(['UNSEEN'])
    for msg_id in messages:
        raw_msg = server.fetch([msg_id], ['RFC822'])[msg_id][b'RFC822']
        msg = email.message_from_bytes(raw_msg)
        subject = msg['Subject']
        sender = msg['From']
        print(f"新着メール: {subject} from {sender}")

    server.logout()

schedule.every(20).minutes.do(check_new_emails)

while True:
    schedule.run_pending()
    time.sleep(10)

ユニットテストの実装例

# calculator.py
class Calculator:
    def add(self, a, b):
        return a + b

    def divide(self, a, b):
        if b == 0:
            raise ValueError("ゼロ除算は許可されていません")
        return a / b

# test_calculator.py
import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
    def setUp(self):
        self.calc = Calculator()

    def test_add_returns_sum(self):
        self.assertEqual(self.calc.add(7, 3), 10)

    def test_divide_by_zero_raises_error(self):
        with self.assertRaises(ValueError):
            self.calc.divide(5, 0)

if __name__ == '__main__':
    unittest.main()

タグ: Python PDF操作 自動化 メール送信 Webスクレイピング

6月8日 22:31 投稿