SWHL commited on
Commit
e912ec1
Β·
1 Parent(s): 2fd92e6

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +115 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- encoding: utf-8 -*-
2
+ # @Author: SWHL
3
+ # @Contact: liekkaskono@163.com
4
+ import shutil
5
+ import tempfile
6
+ import zipfile
7
+ from pathlib import Path
8
+
9
+ import streamlit as st
10
+ import streamlit_scrollable_textbox as stx
11
+ from extract_office_content import ExtractExcel, ExtractPPT, ExtractWord
12
+
13
+ word_extract = ExtractWord()
14
+ ppt_extracter = ExtractPPT()
15
+ excel_extract = ExtractExcel()
16
+
17
+
18
+ def mkdir(dir_path):
19
+ Path(dir_path).mkdir(parents=True, exist_ok=True)
20
+
21
+
22
+ if __name__ == '__main__':
23
+ st.markdown("<h1 style='text-align: center;'><a href='https://github.com/SWHL/ExtractOfficeContent' style='text-decoration: none'>ExtractOfficeContent</a></h1>", unsafe_allow_html=True)
24
+ st.markdown("""
25
+ <p>
26
+ <a href=""><img src="https://img.shields.io/badge/Python->=3.6,<=3.11-aff.svg"></a>
27
+ <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
28
+ <a href="https://pypi.org/project/extract_office_content/"><img alt="PyPI" src="https://img.shields.io/pypi/v/extract_office_content"></a>
29
+ <a href="https://pepy.tech/project/extract_office_content"><img src="https://static.pepy.tech/personalized-badge/extract_office_content?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
30
+ </p>
31
+ """, unsafe_allow_html=True)
32
+
33
+ st.markdown('##### Extract PPT Content')
34
+ uploaded_file = st.file_uploader("Choose a file", type=['ppt', 'pptx'],
35
+ key='ppt', label_visibility='collapsed')
36
+ if uploaded_file is not None:
37
+ bytes_data = uploaded_file.getvalue()
38
+ file_name = Path(uploaded_file.name).stem
39
+
40
+ with tempfile.TemporaryDirectory() as tmp_dir:
41
+ with st.spinner('ζ­£εœ¨ζε–δΈ­....'):
42
+ ppt_res = ppt_extracter(bytes_data, save_img_dir=tmp_dir)
43
+ st.markdown('ζε–η»“ζžœοΌš')
44
+ stx.scrollableTextbox(ppt_res, height=300)
45
+
46
+ zip_dir = Path('ppt_zip_dir')
47
+ mkdir(zip_dir)
48
+
49
+ zip_path = zip_dir / f'{file_name}.zip'
50
+ with zipfile.ZipFile(zip_path, mode='w') as zf:
51
+ for file_path in Path(tmp_dir).rglob('*.*'):
52
+ zf.write(file_path)
53
+
54
+ with open(zip_path, 'rb') as file:
55
+ is_download = st.download_button('Download images',
56
+ data=file,
57
+ file_name=f'{file_name}.zip')
58
+ if is_download:
59
+ shutil.rmtree(zip_dir)
60
+
61
+ st.markdown('##### Extract Word Content')
62
+ uploaded_file = st.file_uploader("Choose a file", type=['doc', 'docx'],
63
+ key='word', label_visibility='collapsed')
64
+ if uploaded_file is not None:
65
+ bytes_data = uploaded_file.getvalue()
66
+ file_name = Path(uploaded_file.name).stem
67
+
68
+ with tempfile.TemporaryDirectory() as word_tmp_dir:
69
+ with st.spinner('ζ­£εœ¨ζε–δΈ­....'):
70
+ word_res = word_extract(bytes_data, save_img_dir=word_tmp_dir)
71
+ st.markdown('ζε–η»“ζžœοΌš')
72
+ stx.scrollableTextbox(word_res, height=300)
73
+
74
+ zip_dir = Path('word_zip_dir')
75
+ mkdir(zip_dir)
76
+
77
+ zip_path = zip_dir / f'{file_name}.zip'
78
+ with zipfile.ZipFile(zip_path, mode='w') as zf:
79
+ for file_path in Path(word_tmp_dir).rglob('*.*'):
80
+ zf.write(file_path)
81
+
82
+ with open(zip_path, 'rb') as file:
83
+ is_download = st.download_button('Download images',
84
+ data=file,
85
+ file_name=f'{file_name}.zip')
86
+ if is_download:
87
+ shutil.rmtree(zip_dir)
88
+
89
+ st.markdown('##### Extract Excel Content')
90
+ uploaded_file = st.file_uploader("Choose a file", type=['xls', 'xlsx'],
91
+ key='excel', label_visibility='collapsed')
92
+ if uploaded_file is not None:
93
+ bytes_data = uploaded_file.getvalue()
94
+ file_name = Path(uploaded_file.name).stem
95
+
96
+ with tempfile.TemporaryDirectory() as word_tmp_dir:
97
+ with st.spinner('ζ­£εœ¨ζε–δΈ­....'):
98
+ word_res = excel_extract(bytes_data, save_img_dir=word_tmp_dir)
99
+ st.markdown('ζε–η»“ζžœοΌš')
100
+ stx.scrollableTextbox(word_res, height=300)
101
+
102
+ zip_dir = Path('excel_zip_dir')
103
+ mkdir(zip_dir)
104
+
105
+ zip_path = zip_dir / f'{file_name}.zip'
106
+ with zipfile.ZipFile(zip_path, mode='w') as zf:
107
+ for file_path in Path(word_tmp_dir).rglob('*.*'):
108
+ zf.write(file_path)
109
+
110
+ with open(zip_path, 'rb') as file:
111
+ is_download = st.download_button('Download images',
112
+ data=file,
113
+ file_name=f'{file_name}.zip')
114
+ if is_download:
115
+ shutil.rmtree(zip_dir)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ altair<5
2
+ lxml>=4.9.1
3
+ openpyxl>=3.1.2
4
+ pandas>=1.3.5
5
+ python_docx>=0.8.11
6
+ python_pptx>=0.6.21
7
+ tabulate
8
+ filetype
9
+ extract_office_content