Spaces:
Sleeping
Sleeping
File size: 5,196 Bytes
e912ec1 |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
import shutil
import tempfile
import zipfile
from pathlib import Path
import streamlit as st
import streamlit_scrollable_textbox as stx
from extract_office_content import ExtractExcel, ExtractPPT, ExtractWord
word_extract = ExtractWord()
ppt_extracter = ExtractPPT()
excel_extract = ExtractExcel()
def mkdir(dir_path):
Path(dir_path).mkdir(parents=True, exist_ok=True)
if __name__ == '__main__':
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)
st.markdown("""
<p>
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<=3.11-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href="https://pypi.org/project/extract_office_content/"><img alt="PyPI" src="https://img.shields.io/pypi/v/extract_office_content"></a>
<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>
</p>
""", unsafe_allow_html=True)
st.markdown('##### Extract PPT Content')
uploaded_file = st.file_uploader("Choose a file", type=['ppt', 'pptx'],
key='ppt', label_visibility='collapsed')
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
file_name = Path(uploaded_file.name).stem
with tempfile.TemporaryDirectory() as tmp_dir:
with st.spinner('ζ£ε¨ζεδΈ....'):
ppt_res = ppt_extracter(bytes_data, save_img_dir=tmp_dir)
st.markdown('ζεη»ζοΌ')
stx.scrollableTextbox(ppt_res, height=300)
zip_dir = Path('ppt_zip_dir')
mkdir(zip_dir)
zip_path = zip_dir / f'{file_name}.zip'
with zipfile.ZipFile(zip_path, mode='w') as zf:
for file_path in Path(tmp_dir).rglob('*.*'):
zf.write(file_path)
with open(zip_path, 'rb') as file:
is_download = st.download_button('Download images',
data=file,
file_name=f'{file_name}.zip')
if is_download:
shutil.rmtree(zip_dir)
st.markdown('##### Extract Word Content')
uploaded_file = st.file_uploader("Choose a file", type=['doc', 'docx'],
key='word', label_visibility='collapsed')
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
file_name = Path(uploaded_file.name).stem
with tempfile.TemporaryDirectory() as word_tmp_dir:
with st.spinner('ζ£ε¨ζεδΈ....'):
word_res = word_extract(bytes_data, save_img_dir=word_tmp_dir)
st.markdown('ζεη»ζοΌ')
stx.scrollableTextbox(word_res, height=300)
zip_dir = Path('word_zip_dir')
mkdir(zip_dir)
zip_path = zip_dir / f'{file_name}.zip'
with zipfile.ZipFile(zip_path, mode='w') as zf:
for file_path in Path(word_tmp_dir).rglob('*.*'):
zf.write(file_path)
with open(zip_path, 'rb') as file:
is_download = st.download_button('Download images',
data=file,
file_name=f'{file_name}.zip')
if is_download:
shutil.rmtree(zip_dir)
st.markdown('##### Extract Excel Content')
uploaded_file = st.file_uploader("Choose a file", type=['xls', 'xlsx'],
key='excel', label_visibility='collapsed')
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
file_name = Path(uploaded_file.name).stem
with tempfile.TemporaryDirectory() as word_tmp_dir:
with st.spinner('ζ£ε¨ζεδΈ....'):
word_res = excel_extract(bytes_data, save_img_dir=word_tmp_dir)
st.markdown('ζεη»ζοΌ')
stx.scrollableTextbox(word_res, height=300)
zip_dir = Path('excel_zip_dir')
mkdir(zip_dir)
zip_path = zip_dir / f'{file_name}.zip'
with zipfile.ZipFile(zip_path, mode='w') as zf:
for file_path in Path(word_tmp_dir).rglob('*.*'):
zf.write(file_path)
with open(zip_path, 'rb') as file:
is_download = st.download_button('Download images',
data=file,
file_name=f'{file_name}.zip')
if is_download:
shutil.rmtree(zip_dir)
|