-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf2xmlv2.py
79 lines (68 loc) · 2.17 KB
/
pdf2xmlv2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import PyPDF2
import docx
import pytesseract
from PIL import Image
import re
import os
import tkinter as tk
from tkinter import filedialog
def pdf_to_xml(file_path):
pdf_file = open(file_path, 'rb')
pdf_reader = PyPDF2.PdfReader(pdf_file)
xml_text = '<document>\n'
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
page_text = page.extract_text()
xml_text += '<page>\n'
xml_text += page_text
xml_text += '</page>\n'
xml_text += '</document>'
return xml_text
def docx_to_xml(file_path):
doc = docx.Document(file_path)
xml_text = '<document>\n'
for para in doc.paragraphs:
xml_text += '<paragraph>\n'
xml_text += para.text
xml_text += '</paragraph>\n'
xml_text += '</document>'
return xml_text
def image_to_text(file_path):
image = Image.open(file_path)
text = pytesseract.image_to_string(image)
text = re.sub(r'[^\x00-\x7F]+', '', text) # remove non-ascii characters
return text
def file_to_xml(file_path):
if file_path.endswith('.pdf'):
xml_text = pdf_to_xml(file_path)
elif file_path.endswith('.docx'):
xml_text = docx_to_xml(file_path)
elif file_path.endswith('.jpg') or file_path.endswith('.png'):
text = image_to_text(file_path)
xml_text = '<document>\n'
xml_text += '<page>\n'
xml_text += text
xml_text += '</page>\n'
xml_text += '</document>'
else:
return None
# Save XML text to file
xml_file_path = os.path.splitext(file_path)[0] + '.xml'
with open(xml_file_path, 'w') as xml_file:
xml_file.write(xml_text)
return xml_text
def browse_file():
file_path = filedialog.askopenfilename()
if file_path:
xml_text = file_to_xml(file_path)
if xml_text:
success_label.config(text='Conversion successful!')
else:
success_label.config(text='Invalid file type')
root = tk.Tk()
root.title('PDF/DOCX to XML Converter')
upload_button = tk.Button(root, text='Upload File', command=browse_file)
upload_button.pack(pady=20)
success_label = tk.Label(root, text='')
success_label.pack()
root.mainloop()