Created
August 26, 2024 22:02
-
-
Save si3mshady/5f6742d2448c22ba143fe2f6a50ba043 to your computer and use it in GitHub Desktop.
Tool that uses a widget to take screenshots and saves them in word document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can create a Python program to automatically add screenshots to a Word document using the `python-docx` library, along with the `pyautogui` library to capture screenshots. Here's a basic script to achieve this: | |
# ### 1. Install Required Libraries | |
# First, you need to install the necessary Python libraries: | |
# ```bash | |
# pip install python-docx pyautogui pillow | |
# ``` | |
# ### 2. Python Script | |
# ```pythonp | |
import pyautogui | |
from docx import Document | |
from docx.shared import Inches | |
import time | |
import os | |
import tkinter as tk | |
from tkinter import messagebox | |
class ScreenshotApp: | |
def __init__(self, root): | |
self.root = root | |
self.root.title("Screenshot to Word") | |
self.doc = Document() | |
# Create the main button | |
self.capture_button = tk.Button(root, text="Capture Screenshot", command=self.capture_screenshot) | |
self.capture_button.pack(pady=20) | |
# Create the save button | |
self.save_button = tk.Button(root, text="Save Document", command=self.save_document) | |
self.save_button.pack(pady=10) | |
# Create the quit button | |
self.quit_button = tk.Button(root, text="Quit", command=self.quit_app) | |
self.quit_button.pack(pady=10) | |
def capture_screenshot(self): | |
timestamp = time.strftime("%Y%m%d-%H%M%S") | |
image_filename = f"screenshot_{timestamp}.png" | |
# Capture the screenshot | |
screenshot = pyautogui.screenshot() | |
screenshot.save(image_filename) | |
# Add the screenshot to the document | |
self.doc.add_picture(image_filename, width=Inches(6)) | |
self.doc.add_page_break() | |
# Show a confirmation message | |
messagebox.showinfo("Screenshot Captured", "Screenshot added to the document.") | |
# Clean up the image file | |
os.remove(image_filename) | |
def save_document(self): | |
output_filename = "Screenshots.docx" | |
self.doc.save(output_filename) | |
messagebox.showinfo("Document Saved", f"Word document saved as {output_filename}") | |
def quit_app(self): | |
# Ask for confirmation before quitting | |
if messagebox.askokcancel("Quit", "Do you really want to quit?"): | |
self.root.destroy() | |
if __name__ == "__main__": | |
root = tk.Tk() | |
app = ScreenshotApp(root) | |
root.mainloop() | |
# ### How It Works: | |
# 1. **Capture Screenshot**: The script captures a screenshot using `pyautogui.screenshot()` and saves it with a timestamped filename. | |
# 2. **Add to Word Document**: The screenshot is added to a Word document using the `add_picture` method from `python-docx`. | |
# 3. **Loop**: The script will continue capturing screenshots and adding them to the document until you decide to quit by typing `q`. | |
# 4. **Save and Clean Up**: The document is saved as `Screenshots.docx`, and the temporary screenshot files are optionally deleted. | |
# ### Usage: | |
# 1. Run the script. | |
# 2. Each time you press `Enter`, a screenshot will be taken and added to the Word document. | |
# 3. Type `q` and press `Enter` to stop the script and save the document. | |
# This script is a basic implementation and can be customized further to fit specific needs, like adding timestamps, captions, or specific formatting for the screenshots in the Word document. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment