├── requirements.txt ├── run_app.py ├── config.py ├── LICENSE ├── README.md ├── main.py └── TKGUI.py /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit==1.25.0 2 | pdf2image==1.16.3 3 | Pillow==9.2.0 4 | poppler-utils==0.86.1 -------------------------------------------------------------------------------- /run_app.py: -------------------------------------------------------------------------------- 1 | import streamlit.web.cli as stcli # 修改了导入模块 2 | import sys 3 | 4 | if __name__ == '__main__': 5 | sys.argv = ["streamlit", "run", "main.py"] 6 | sys.exit(stcli.main()) -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | 4 | # 输出目录 5 | OUTPUT_DIR = "output" 6 | 7 | # Poppler 路径 8 | if sys.platform.startswith('win'): 9 | POPPLER_PATH = os.path.join("poppler", "poppler-24.07.0", "Library", "bin") 10 | # 默认poppler路径是当前目录 11 | else: 12 | POPPLER_PATH = "/usr/bin" # 根据实际安装路径修改 13 | 14 | # LibreOffice 路径 15 | if sys.platform.startswith('win'): 16 | LIBREOFFICE_PATH = os.path.join('.', 'libreoffice', 'App', 'libreoffice', 'program', 'soffice.exe') 17 | # 默认libreoffice路径是当前目录 18 | else: 19 | LIBREOFFICE_PATH = "/usr/bin/libreoffice" # 根据实际安装路径修改 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 yr2b 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # File2LongImage 2 | # 文件转长图工具 3 | 4 | ## 简介 5 | 这个工具是一个用于将多种文件格式(如 PDF、Word、Excel、PPT 等)转换为长图片的工具。通过该工具,你可以上传一个文件,工具会自动将其转换为 PDF 并生成一张合并后的长图。这个项目使用 [Streamlit](https://streamlit.io/) 作为前端展示界面,使用 [pdf2image](https://github.com/Belval/pdf2image) 进行 PDF 到图片的转换,并使用 [Pillow](https://python-pillow.org/) 来处理图片的合并。 6 | 7 | ## 功能 8 | - 支持多种文件格式:PDF、Word、Excel、PPT 等。 9 | - 将文件转换为 PDF 并自动转换为图片。 10 | - 合并多张图片为长图,便于查看和分享。 11 | - 可设置图片的 DPI(PPI)以调整输出图片的质量。 12 | 13 | ## 依赖环境 14 | 要运行此项目,请确保已安装以下依赖: 15 | - [Streamlit](https://streamlit.io/):用于构建交互式 Web 应用程序。 16 | - [pdf2image](https://github.com/Belval/pdf2image):将 PDF 转换为图像的库。 17 | - [Pillow](https://python-pillow.org/):图像处理库。 18 | - [poppler-utils](https://poppler.freedesktop.org/):用于支持 PDF 转换的工具。 19 | 20 | ### 如不熟悉配置流程,也可直接下载打包好的文件运行,基于tkinter,所以页面不如streamlit精美 21 | ### 环境及可执行文件下载地址: 22 | 23 | #### [夸克盘](https://pan.quark.cn/s/a5d0e37115a8) 24 | 25 | #### [百度盘,提取码69hi](https://pan.baidu.com/s/1p6reebYtEnxt0od-BxIxyQ?pwd=69hi) 26 | 27 | ## 安装步骤 28 | 1. 克隆项目到本地: 29 | ```bash 30 | git clone https://github.com/你的用户名/文件转长图工具.git 31 | cd 文件转长图工具 32 | ``` 33 | 34 | 2. 安装所需的 Python 库: 35 | ```bash 36 | pip install -r requirements.txt 37 | ``` 38 | 39 | 3. 安装 Poppler: 40 | - **Windows**: 下载 [Poppler for Windows](http://blog.alivate.com.au/poppler-windows/),并将其路径添加到系统环境变量,或者在 `config.py` 中设置 `POPPLER_PATH`。 41 | - **macOS**: 使用 Homebrew 安装: 42 | ```bash 43 | brew install poppler 44 | ``` 45 | 并在 `config.py` 中设置 `POPPLER_PATH`,如 `/usr/local/bin`。 46 | - **Linux**: 使用包管理器安装(以 Ubuntu 为例): 47 | ```bash 48 | sudo apt-get install poppler-utils 49 | ``` 50 | 并在 `config.py` 中设置 `POPPLER_PATH`,如 `/usr/bin`。 51 | 52 | 4. 安装 LibreOffice(如果需要转换非 PDF 文件,如 Word、Excel、PPT): 53 | - **Windows**: 下载并安装 [LibreOffice](https://www.libreoffice.org/download/download/),并在 `config.py` 中设置 `LIBREOFFICE_PATH`。 54 | - **Linux**/ **macOS**: 55 | ```bash 56 | sudo apt install libreoffice 57 | ``` 58 | 并在 `config.py` 中设置 `LIBREOFFICE_PATH`,如 `/usr/bin/libreoffice`。 59 | 60 | 5. 配置文件 61 | - 编辑 `config.py` 文件,设置以下参数: 62 | - `OUTPUT_DIR`: 输出目录的路径。 63 | - `POPPLER_PATH`: Poppler 的安装路径。 64 | - `LIBREOFFICE_PATH`: LibreOffice 的安装路径。 65 | 66 | ## 运行方法 67 | 1. 在项目目录下运行以下命令启动应用: 68 | ```bash 69 | streamlit run main.py 70 | ``` 71 | 72 | 2. 打开浏览器并访问 `http://localhost:8501`,即可看到应用界面。 73 | 74 | 3. 上传需要转换的文件,并设置图片 DPI。等待文件转换完成后,页面将展示合并后的长图。 75 | 76 | 77 | 78 | ## 文件说明 79 | - `main.py`: 主程序文件,处理文件上传、格式转换和图像合并等功能。 80 | - `requirements.txt`: 项目所需的依赖库清单。 81 | - `config.py`: 配置文件,包含所有的目录配置。 82 | - `run_app.py`: 运行此py文件可直接运行程序 83 | - `TKGUI.py`: 使用tkinter重新构建的程序,不通过网页运行,更方便打包和本地使用。 84 | 85 | ## 注意事项 86 | - **系统支持**:目前仅在windows下进行过测试,其他系统环境可能需要自行适配 87 | - **文件大小**:请注意上传文件的大小,因为转换和合并较大的文件可能会耗费较长时间。 88 | - **格式支持**:该工具依赖 LibreOffice 进行非 PDF 文件的转换,因此确保安装了正确版本的 LibreOffice。 89 | - **图片质量**:通过调整 DPI 来控制输出图片的质量,DPI 越高,图片越清晰,但文件大小也会增大。 90 | 91 | ## 以上readme文件主要由o1生成,如有错误,请指出 92 | 93 | ## 开源许可 94 | 该项目基于 MIT 开源许可进行分发。详情请查看 [LICENSE](LICENSE)。 95 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pdf2image 3 | import streamlit as st 4 | from PIL import Image 5 | import time 6 | import subprocess 7 | import sys 8 | from config import OUTPUT_DIR, POPPLER_PATH, LIBREOFFICE_PATH 9 | 10 | st.set_page_config(page_title="文件转长图工具", page_icon="🖼️") 11 | 12 | def merge_images(images, output_path, output_format="PNG", quality=85): 13 | st.write("开始合并图像...") 14 | progress_bar = st.progress(0) 15 | status_text = st.empty() 16 | start_time = time.time() 17 | 18 | widths, heights = zip(*(i.size for i in images)) 19 | total_height = sum(heights) 20 | max_width = max(widths) 21 | 22 | merged_image = Image.new('RGB', (max_width, total_height)) 23 | y_offset = 0 24 | 25 | total_images = len(images) 26 | for idx, img in enumerate(images): 27 | merged_image.paste(img, (0, y_offset)) 28 | y_offset += img.height 29 | 30 | # 更新进度 31 | progress = (idx + 1) / total_images 32 | progress_bar.progress(progress) 33 | elapsed_time = time.time() - start_time 34 | estimated_total_time = elapsed_time / progress 35 | remaining_time = estimated_total_time - elapsed_time 36 | status_text.text(f"正在合并图像:{idx + 1}/{total_images},预计剩余时间:{int(remaining_time)}秒") 37 | 38 | # 保存并压缩图像 39 | if output_format == "JPG": 40 | merged_image = merged_image.convert("RGB") # 确保是 RGB 模式 41 | merged_image.save(output_path, format="JPEG", quality=quality) 42 | else: 43 | merged_image.save(output_path, format="PNG", optimize=True) 44 | 45 | status_text.text("图像合并并压缩完成!") 46 | progress_bar.progress(1.0) 47 | 48 | def convert_to_image(file_path, output_dir, dpi, output_format="PNG", quality=85): 49 | st.write("开始转换文件...") 50 | progress_bar = st.progress(0) 51 | status_text = st.empty() 52 | start_time = time.time() 53 | 54 | images = [] 55 | base_name = os.path.splitext(os.path.basename(file_path))[0] 56 | 57 | if file_path.lower().endswith('.pdf'): 58 | images = pdf2image.convert_from_path(file_path, poppler_path=POPPLER_PATH, dpi=dpi) 59 | progress_bar.progress(0.3) 60 | elif file_path.lower().endswith((".doc", ".docx", ".ppt", ".pptx", ".csv", ".xls", ".xlsx", ".odt", ".rtf", ".txt", ".psd", ".cdr", ".wps", ".svg")): 61 | pdf_path = os.path.join(output_dir, f"{base_name}.pdf") 62 | if sys.platform.startswith('win'): 63 | conversion_cmd = f'"{LIBREOFFICE_PATH}" --headless --convert-to pdf "{file_path}" --outdir "{output_dir}"' 64 | else: 65 | conversion_cmd = f'{LIBREOFFICE_PATH} --headless --convert-to pdf "{file_path}" --outdir "{output_dir}"' 66 | subprocess.run(conversion_cmd, shell=True) 67 | 68 | if not os.path.exists(pdf_path): 69 | raise ValueError("文件转换为 PDF 失败") 70 | else: 71 | status_text.text(f"文件转换为 PDF 成功,正在转换为图像: {pdf_path}") 72 | progress_bar.progress(0.6) 73 | 74 | images = pdf2image.convert_from_path(pdf_path, poppler_path=POPPLER_PATH, dpi=dpi) 75 | progress_bar.progress(0.9) 76 | else: 77 | raise ValueError("不支持的文件格式") 78 | 79 | if images: 80 | merged_output_path = os.path.join(output_dir, f"{base_name}.{output_format.lower()}") 81 | merge_images(images, merged_output_path, output_format, quality) 82 | progress_bar.progress(1.0) 83 | status_text.text("文件转换完成!") 84 | 85 | st.title("文件转长图工具") 86 | 87 | 88 | 89 | dpi = st.slider("设置图片 PPI", min_value=72, max_value=600, value=300) 90 | output_format = st.selectbox("选择输出格式", ["JPG", "PNG"]) 91 | quality = st.slider("设置 JPG 质量", min_value=1, max_value=100, value=85) if output_format == "JPG" else None 92 | 93 | uploaded_file = st.file_uploader("上传文件", type=["pdf", "doc", "docx", "ppt", "pptx", "csv", "xls", "xlsx", "odt", "rtf", "txt", "psd", "cdr", "wps", "svg"]) 94 | 95 | 96 | 97 | if uploaded_file is not None: 98 | os.makedirs(OUTPUT_DIR, exist_ok=True) 99 | with open(uploaded_file.name, "wb") as f: 100 | f.write(uploaded_file.getbuffer()) 101 | convert_to_image(uploaded_file.name, OUTPUT_DIR, dpi, output_format, quality) 102 | st.image(os.path.join(OUTPUT_DIR, f"{os.path.splitext(uploaded_file.name)[0]}.{output_format.lower()}"), caption='转换后的长图') 103 | 104 | 105 | st.markdown( 106 | """ 107 |
110 |
111 |