-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
159 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,162 @@ | ||
import tkinter as tk | ||
from tkinter import filedialog, ttk | ||
import sys | ||
import base64 | ||
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QFileDialog, QMessageBox | ||
from PyQt5.QtCore import Qt | ||
from PyQt5.QtGui import QDragEnterEvent, QDropEvent, QPixmap, QIcon | ||
from PIL import Image | ||
import pytesseract | ||
import docx | ||
|
||
def image_to_word(input_entry, output_entry, status_label): | ||
"""Converts an image to a Word document.""" | ||
|
||
input_file = input_entry.get() | ||
output_file = output_entry.get() | ||
|
||
try: | ||
img = Image.open(input_file) | ||
text = pytesseract.image_to_string(img) | ||
|
||
doc = docx.Document() | ||
doc.add_paragraph(text) | ||
doc.save(output_file) | ||
|
||
status_label.config(text="Conversion successful!") | ||
except Exception as e: | ||
status_label.config(text=f"Error: {str(e)}") | ||
|
||
def select_input_file(entry): | ||
"""Selects input image file.""" | ||
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.jpeg;*.png;*.bmp")]) | ||
entry.delete(0, tk.END) | ||
entry.insert(0, file_path) | ||
|
||
def select_output_file(entry): | ||
"""Selects output Word file.""" | ||
file_path = filedialog.asksaveasfilename(defaultextension=".docx", filetypes=[("Word documents", "*.docx")]) | ||
entry.delete(0, tk.END) | ||
entry.insert(0, file_path) | ||
|
||
def create_gui(): | ||
"""Creates the GUI.""" | ||
|
||
root = tk.Tk() | ||
root.title("Image to Word Converter") | ||
|
||
# Input file frame | ||
input_frame = ttk.Frame(root) | ||
input_frame.pack(pady=10) | ||
|
||
input_label = ttk.Label(input_frame, text="Input Image:") | ||
input_label.pack(side="left") | ||
input_entry = ttk.Entry(input_frame, width=30) | ||
input_entry.pack(side="left") | ||
input_button = ttk.Button(input_frame, text="Browse", command=lambda: select_input_file(input_entry)) | ||
input_button.pack(side="left") | ||
|
||
# Output file frame | ||
output_frame = ttk.Frame(root) | ||
output_frame.pack(pady=10) | ||
|
||
output_label = ttk.Label(output_frame, text="Output Word:") | ||
output_label.pack(side="left") | ||
output_entry = ttk.Entry(output_frame, width=30) | ||
output_entry.pack(side="left") | ||
output_button = ttk.Button(output_frame, text="Browse", command=lambda: select_output_file(output_entry)) | ||
output_button.pack(side="left") | ||
|
||
# Conversion button | ||
convert_button = ttk.Button(root, text="Convert", command=lambda: image_to_word(input_entry, output_entry, status_label)) | ||
convert_button.pack(pady=10) | ||
|
||
# Status label | ||
status_label = ttk.Label(root, text="") | ||
status_label.pack() | ||
|
||
root.mainloop() | ||
|
||
if __name__ == "__main__": | ||
create_gui() | ||
import io | ||
|
||
class ImageToWordConverter(QWidget): | ||
def __init__(self): | ||
super().__init__() | ||
self.init_ui() | ||
self.set_styles() # Set the styles for the UI | ||
self.set_window_icon() # Set the window icon | ||
|
||
def init_ui(self): | ||
self.setWindowTitle('Image to Word Converter') | ||
|
||
# Input field for image file | ||
self.input_label = QLabel('Input Image:') | ||
self.input_entry = QLineEdit(self) | ||
self.input_entry.setPlaceholderText("Drag & Drop or Browse Image") | ||
self.input_entry.setAcceptDrops(True) | ||
|
||
# Enable drag and drop | ||
self.setAcceptDrops(True) | ||
|
||
self.input_button = QPushButton('Browse') | ||
self.input_button.clicked.connect(self.select_input_file) | ||
|
||
# Output field for Word file path | ||
self.output_label = QLabel('Output Directory:') | ||
self.output_entry = QLineEdit(self) | ||
self.output_button = QPushButton('Browse') | ||
self.output_button.clicked.connect(self.select_output_directory) | ||
|
||
# Convert button | ||
self.convert_button = QPushButton('Convert') | ||
self.convert_button.clicked.connect(self.convert_image_to_word) | ||
|
||
# Status label | ||
self.status_label = QLabel('') | ||
|
||
# Layout | ||
layout = QVBoxLayout() | ||
layout.addWidget(self.input_label) | ||
layout.addWidget(self.input_entry) | ||
layout.addWidget(self.input_button) | ||
layout.addWidget(self.output_label) | ||
layout.addWidget(self.output_entry) | ||
layout.addWidget(self.output_button) | ||
layout.addWidget(self.convert_button) | ||
layout.addWidget(self.status_label) | ||
self.setLayout(layout) | ||
|
||
# Set window dimensions | ||
self.setGeometry(300, 300, 400, 300) | ||
|
||
def set_styles(self): | ||
self.setStyleSheet(""" | ||
QWidget { | ||
background-color: #2C2F33; /* Dark background */ | ||
color: #FFFFFF; /* White text */ | ||
font-family: Arial, sans-serif; | ||
} | ||
QLabel { | ||
font-size: 16px; | ||
margin-bottom: 5px; | ||
} | ||
QLineEdit { | ||
background-color: #23272A; /* Dark input field */ | ||
color: #FFFFFF; | ||
border: 1px solid #7289DA; /* Light blue border */ | ||
padding: 5px; | ||
border-radius: 5px; | ||
} | ||
QPushButton { | ||
background-color: #7289DA; /* Blue button */ | ||
color: #FFFFFF; | ||
border: none; | ||
padding: 10px; | ||
border-radius: 5px; | ||
font-size: 14px; | ||
margin-top: 10px; | ||
} | ||
QPushButton:hover { | ||
background-color: #5B6EAE; /* Darker blue on hover */ | ||
} | ||
QLabel#status_label { | ||
margin-top: 20px; | ||
font-weight: bold; | ||
} | ||
""") | ||
|
||
def set_window_icon(self): | ||
# Base64 encoded icon image (replace with your actual base64 string) | ||
base64_icon = """ | ||
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAA7AAAAOwBeShxvQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAPrSURBVFiF5ZVtTBxFHMZ/e7vXu+VMBBSaaltSAxXElGpLX8BPNZwtLUSFKFbaCE0vIvEFJGhi2iZGkxqNtFZqS2NMfKlf/KC1lQbO2lp5rQiIVFuqTbBXqTFVhOP2ure7flC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seEdN3adWRkZESMRb18QlooJuQAmq5FjUV1ICszi6zMrJiJuGYB/5sijFgDhmFEGo4L/rYFhmHg908gSWKk+f8YgiBYkrYI0HWdpgNNcSE2CbBsuzAtvgG4K64K4CRwZLLzB4HglqPCtzY+AAAAAElFTkSuQmCC | ||
""" # Example base64 image (a tiny 1x1 pixel) | ||
|
||
# Decode the base64 string and create a QPixmap | ||
icon_data = base64.b64decode(base64_icon) | ||
icon_pixmap = QPixmap() | ||
icon_pixmap.loadFromData(icon_data) | ||
|
||
# Set the window icon directly from the QPixmap | ||
self.setWindowIcon(QIcon(icon_pixmap)) | ||
|
||
# Enable drag and drop for the input entry | ||
def dragEnterEvent(self, event: QDragEnterEvent): | ||
if event.mimeData().hasUrls(): | ||
event.acceptProposedAction() | ||
|
||
def dropEvent(self, event: QDropEvent): | ||
if event.mimeData().hasUrls(): | ||
file_url = event.mimeData().urls()[0] | ||
file_path = file_url.toLocalFile() | ||
self.input_entry.setText(file_path) | ||
|
||
def select_input_file(self): | ||
file_path, _ = QFileDialog.getOpenFileName(self, 'Select Image File', '', 'Image files (*.jpg *.jpeg *.png *.bmp)') | ||
if file_path: | ||
self.input_entry.setText(file_path) | ||
|
||
def select_output_directory(self): | ||
dir_path = QFileDialog.getExistingDirectory(self, 'Select Output Directory') | ||
if dir_path: | ||
self.output_entry.setText(dir_path) | ||
|
||
def convert_image_to_word(self): | ||
input_file = self.input_entry.text() | ||
output_directory = self.output_entry.text() | ||
|
||
if not input_file or not output_directory: | ||
self.status_label.setText("Please provide both input and output directories.") | ||
return | ||
|
||
# Create the output file path | ||
output_file = os.path.join(output_directory, 'output.docx') | ||
|
||
try: | ||
img = Image.open(input_file) | ||
text = pytesseract.image_to_string(img) | ||
|
||
doc = docx.Document() | ||
doc.add_paragraph(text) | ||
doc.save(output_file) | ||
|
||
self.status_label.setText(f"Conversion successful! Saved to {output_file}") | ||
except Exception as e: | ||
self.status_label.setText(f"Error: {str(e)}") | ||
|
||
def main(): | ||
app = QApplication(sys.argv) | ||
window = ImageToWordConverter() | ||
window.show() | ||
sys.exit(app.exec_()) | ||
|
||
if __name__ == '__main__': | ||
main() |