screenshot.html

Python
tests.py(スクショ範囲の左上と右下の座標を調べる)
import pyautogui as pgx,y = pg.position()print(‘x座標’,x)print(‘y座標は’,y)
Google APIの有効化
https://laboratory.kazuuu.net/upload-files-to-google-drive-with-python/をもとにclient_secrets.jsonファイルをカレントディレクトリに入れるこのとき、OAuth同意画面でアプリ名等を入力し、そのあとの画面でTest Userを追加することこれをしないとプログラムを実行した時「アクセスをブロックMyAppはGoogleの審査プロセスが完了していません」というエラーになるhttps://teratail.com/questions/qejycrwrvjvgyk
models.py
class Screenshot(models.Model):path = models.CharField(max_length=100,default=’C:/Users/wiki1/Desktop/screenshot’)x1 = models.IntegerField(blank=False,default=’1′)y1 = models.IntegerField(blank=False,default=’1′)x2 = models.IntegerField(blank=False,default=’1000′)y2 = models.IntegerField(blank=False,default=’1000′)page = models.IntegerField(blank=False,default=’10’)
def __str__(self):return self.path
views.py(見開きでスクショする場合)
pip install pyautoguipip install PyDrive
from pydrive.drive import GoogleDrivefrom pydrive.auth import GoogleAuthimport pyautoguiimport timeimport os

def screenshot(request):    if request.method == ‘POST’:        form = ScreenshotForm(request.POST)        if form.is_valid():            form.save()            path = request.POST[‘path’]            x1 = request.POST[‘x1’]            y1 = request.POST[‘y1’]            x2 = request.POST[‘x2’]            y2 = request.POST[‘y2’]            page = request.POST[‘page’]
            #デスクトップにフォルダを作成            time.sleep(5)            if not os.path.exists(path):                os.makedirs(path)
            #スクショ            for p in range(int(page)):                s = pyautogui.screenshot(region=(int(x1), int(y1), int(x2) – int(x1), int(y2) – int(y1)))                s.save(path + ‘/’ + str(p+1).zfill(3) + ‘.png’)                pyautogui.keyDown(‘right’)                time.sleep(2)                        #Googleサービスを認証            ga = GoogleAuth()
            #OAuthクライアントのcredentialsファイルをロード            ga.LoadCredentialsFile(‘mycreds.txt’)
            #Googleサービスの認証            #credentialsファイルがない場合は、ローカルサーバを起動し認証フローを行いアクセストークンを取得            #アクセストークン(=認証コード)が期限切れの場合はアクセストークンのリフレッシュ            if ga.credentials is None:                ga.LocalWebserverAuth()            elif ga.access_token_expired:                ga.Refresh()            else:                ga.Authorize()                        #資格情報をtxtファイルに保存            ga.SaveCredentialsFile(‘mycreds.txt’)
            #認証処理を行う            drive = GoogleDrive(ga)
            #ドライブ上にフォルダを作成            #mimeTypeはmineTypeじゃないので注意            #mineTypeにするとフォルダではなくbinaryファイルが作成される            title = path.split(‘/’)[-1]            folder = drive.CreateFile({‘title’:title,’mimeType’:’application/vnd.google-apps.folder’})            folder.Upload()                        #フォルダのIDを取得            query = ‘title = “‘ + title + ‘”‘            folder_id = drive.ListFile({‘q’: query}).GetList()[0][‘id’]            print(‘idは’,folder_id)
            #ドライブにアップロード            for i in os.listdir(path):                file = drive.CreateFile({                    ‘title’: i,                    ‘parents’: [{‘id’: folder_id}]                })                file.SetContentFile(os.path.join(path,i))                file.Upload()                file = None                        return redirect(‘app:index’)    else:        form = ScreenshotForm()    return render(request,’app/screenshot.html’,{‘form’:form})
1ページずつスクショする場合
pip install pyautogui
import pyautogui as pgimport timeimport os
# 変数定義(左上座標と右下座標)x1, y1 = 290,100×2, y2 = 960,1025×3, y3 = 960,100×4, y4 = 1630,1025
# この間にスクショするウィンドウをアクティブにするtime.sleep(5)os.makedirs(‘C:/Users/wiki1/Desktop/screenshot’)
# ページ数分スクショするfor p in range(100): s = pg.screenshot(region=(x1, y1, x2 – x1, y2 – y1)) s.save(‘C:/Users/wiki1/Desktop/screenshot/’ + str(p * 2 – 1).zfill(3) + ‘.png’) s = pg.screenshot(region=(x3, y3, x4 – x3, y4 – y3)) s.save(‘C:/Users/wiki1/Desktop/screenshot/’ + str(p * 2).zfill(3) + ‘.png’) pg.keyDown(‘right’) time.sleep(2)

cd app

python pg.py で実行するだけ

2023/6/24追記

紀伊国屋の電子リーダーKinoppyはスクショ機能を無効化しているため、

パソコン画面をスクショすると真っ黒な画面だけが写る

BACK