我正在做一些测试活动,这需要我捕捉应用程序/数据库等的屏幕截图,并将其保存到一个文档中。整个活动有50多张截图。在Python中有没有一种方法可以让我用Windows快捷键截图(例如; CTRL ALT Shift C),并将图像附加到文档文件中。我认为Python程序应该像Unix中的nohup一样在后台运行。
zfycwa2u1#
若要使用热键在Word中存储屏幕截图,可以使用库的组合。
要使此脚本工作,您需要在运行脚本之前创建Word文档。
# Need these libraries # pip install keyboard # pip install PyAutoGUI # pip install python-docx # pip install win32gui import keyboard import pyautogui from docx import Document from docx.shared import Inches import win32gui from PIL import ImageGrab shotfile = "C:/tmp/shot.png" # temporary image storage docxfile = "C:/tmp/shots.docx" # main document hotkey = 'ctrl+shift+q' # use this combination anytime while script is running def do_cap(): try: print ('Storing capture...') hwnd = win32gui.GetForegroundWindow() # active window bbox = win32gui.GetWindowRect(hwnd) # bounding rectangle # capture screen shot = pyautogui.screenshot(region=bbox) # take screenshot, active app # shot = pyautogui.screenshot() # take screenshot full screen shot.save(shotfile) # save screenshot # append to document. Doc must exist. doc = Document(docxfile) # open document doc.add_picture(shotfile, width=Inches(7)) # add image, 7 inches wide doc.save(docxfile) # update document print ('Done capture.') except Exception as e: # allow program to keep running print("Capture Error:", e) keyboard.add_hotkey(hotkey, do_cap) # set hot keys print("Started. Waiting for", hotkey) keyboard.wait() # Block forever
2nc8po8w2#
同样的代码工作时做了一些小的变化。谢谢你上面的代码。然而,面对一些挑战,并整理了一些事情,下面的代码工作得很好。屏幕截图是用时间戳捕获的。
hotkey = 'win+prtscn' # use this combination anytime while script is running
shot = pyautogui.screenshot() # to take screenshot full screen with timestamp
docxfile = r'C:\tmp\shots.docx' # main document ADDED THIS STATEMENT
只有上面添加或更改的语句才能完美地工作。也可以使用热键Windows徽标沿着打印机屏幕。当代码处于调试模式时,win+prntscrn将把图像存储在代码中指定的位置以及默认位置pictures screenshots文件夹中停止调试模式后,win+ prntlogn将只在默认位置的screenshots文件夹中存储图像。然而,热键cntrl+shift+q当停止调试模式后使用,然后shot.png图像将有一个没有时间戳的图像.在调试模式下,当使用上述热键时,shot.png将有一个带有时间戳的图像沿着。
win+prntscrn
cntrl+shift+q
shot.png
jbose2ul3#
要新建空白单据,请执行以下操作:
except: document = docx.Document() #document.save('CKS1.docx') document.save('C:/tmp/CKS0.docx') print ('Doc created.') # Doc created #doc1 = Document('CKS0.docx') # open document #doc1.add_picture(shotfile, width=Inches(7)) # add image, 7 inches wide #doc1.save('CKS0.docx') # update document print ('Done capture.') print("Previous file was corrupted or didn't exist - new file was created.")
3条答案
按热度按时间zfycwa2u1#
若要使用热键在Word中存储屏幕截图,可以使用库的组合。
要使此脚本工作,您需要在运行脚本之前创建Word文档。
2nc8po8w2#
同样的代码工作时做了一些小的变化。谢谢你上面的代码。然而,面对一些挑战,并整理了一些事情,下面的代码工作得很好。屏幕截图是用时间戳捕获的。
使用时间戳全屏截图
添加到文档。文件必须存在。
只有上面添加或更改的语句才能完美地工作。也可以使用热键Windows徽标沿着打印机屏幕。当代码处于调试模式时,
win+prntscrn
将把图像存储在代码中指定的位置以及默认位置pictures screenshots文件夹中停止调试模式后,win+ prntlogn将只在默认位置的screenshots文件夹中存储图像。
然而,热键
cntrl+shift+q
当停止调试模式后使用,然后shot.png图像将有一个没有时间戳的图像.在调试模式下,当使用上述热键时,shot.png
将有一个带有时间戳的图像沿着。jbose2ul3#
要新建空白单据,请执行以下操作: