#目的
ウィンドウのスクショ → EXCEL貼り付け
借り物多数
pip install pywin32
#exe化
pyinstaller XXX.py --onefile
#RuntimeError: maximum recursion depth exceeded while calling a Python object
pip uninstall openpyxl
pip install openpyxl==2.3.5
で対処
ウィンドウのスクショ.py
import datetime
import ctypes
from ctypes import sizeof
from ctypes.wintypes import RECT
import win32gui
from PIL import ImageGrab
import keyboard
import mouse
import csv
print('撮りたいウィンドウをクリック')
handle = None
text = ''
#list to csv出力
def export_list_csv(export_list, csv_dir):
try:
with open(csv_dir, "w") as f:
writer = csv.writer(f, lineterminator='\n')
if isinstance(export_list[0], list): #多次元の場合
writer.writerows(export_list)
else:
writer.writerow(export_list)
except:
print("エラー発生")
pass
def mouse_callback():
global handle
global text
handle = win32gui.GetForegroundWindow()
text = win32gui.GetWindowText(handle)
mouse.on_click(mouse_callback)
while not text:
mouse.wait(button='left', target_types=('up',))
print('このウィンドウを撮る: ' + text)
print('違うなら再起動')
rect = win32gui.GetWindowRect(handle)
memory_box = []
def key_callback():
grabed_image = ImageGrab.grab()
croped_image = grabed_image.crop(rect)
name = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')
filename = name + '.jpg'
memory_box.append(filename)
croped_image.save(filename)
keyboard.add_hotkey('print screen', key_callback,
suppress=False)
print('escキーで終了')
keyboard.wait('esc')
export_list_csv(memory_box,"memory.txt")
EXCEL貼り付け.py
import openpyxl
from PIL import Image
import csv
f = open("memory.txt")
data3 = f.read()
f.close()
lines3 = data3.replace('\n','').split(',')
memory_box = [x for x in lines3 if x]
print(memory_box)
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
cell_number = 0
for i in range(len(memory_box)):
im = Image.open('{}'.format(memory_box[i]))
w = im.width
h = im.height
img = openpyxl.drawing.image.Image('{}'.format(memory_box[i]))
if i == 0:
ws.add_image( img, "A1")
cell_number = int( + h // 13.5)
else:
print(cell_number)
ws.add_image( img, "A{}".format(int(cell_number)))
cell_number = int(h // 13.5 + cell_number)
wb.save('result.xlsx')