MinerU2将PDF转成md文件,并分拣图片
首先自行安装python3,必须大于3.10以上的版本
博主是已经有了python3,这里就不演示了
1.先跟新下依赖
pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple
pip install uv -i https://mirrors.aliyun.com/pypi/simple 创建虚拟环境
uv venv激活虚拟环境
.venv\Scripts\activate下载MinerU2核心版
# Windows、Linux和macOS系统无显卡
uv pip install -U "mineru[core]" -i https://mirrors.aliyun.com/pypi/simple# Linux和macOS系统并且有显卡,显存大于8G 安装下面这个
uv pip install -U "mineru[all]" -i https://mirrors.aliyun.com/pypi/simpleWindows 必须安装 PyTorch
有显卡的
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124无显卡
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu操作后验证
mineru --version下载模型
mineru-models-download- 有梯子,如果是默认的直接回车
- 没有梯子,输入
modelscope并回车 - 提示
Please select the model type to download: (pipeline, vlm, all) [all]时,直接回车选择all(下载所有模型) - 模型默认下载到
C:\Users\用户名\.mineru\models目录,可通过mineru.json配置路径。
下载报错超时,有时候不知道什么时候设置了使用国内镜像,临时关闭国内镜像(用官方源)
# Windows(cmd/PowerShell)
set HF_ENDPOINT=https://huggingface.co# 然后重新运行模型下载命令
mineru-models-download下载报错超时,没梯子的用 ModelScope 镜像
# 临时切换(单次生效)
set HF_ENDPOINT=https://modelscope.cn/api/v1/models# 永久配置(系统环境变量中添加 HF_ENDPOINT = https://modelscope.cn/api/v1/models) 直接的用法是用 mineru 解析 PDF / 图片,提取文本、公式、表格等内容
mineru -p "你的文件路径" -o "输出目录" -b 后端名称 -d 设备- 参数说明:
-p:指定要处理的文件路径(支持 PDF、图片等,比如test.pdf或image.png)。-o:指定输出目录,解析后的文本、结构化数据会存到这里。-b:选择解析后端,结合你之前提到的,常用pipeline(兼容性好,无显卡也能用 )、vlm-transformers(依赖 GPU )、vlm-sglang(需特定环境,比如 WSL2 + GPU )。-d:指定推理设备,cpu或cuda(有 NVIDIA 显卡且装了对应驱动、PyTorch 时用cuda加速 )。
示例(无显卡,用 pipeline 后端)
mineru -p "C:\Users\Administrator\test.pdf" -o "C:\Users\Administrator\output" -b pipeline -d cpu启动可视化界面(更直观操作)
mineru-gradio --server-name 127.0.0.1 --server-port 7860结合具体需求扩展
mineru -p "test.pdf" -o "output" -b pipeline -d cpu -f true -t true这里 -f true 强制公式识别,-t true 强制表格识别,确保这两部分内容被重点解析。
或者使用视图窗口 前提安装Gradio
pip install gradio启动视图
mineru-gradio --server-name 127.0.0.1 --server-port 7860访问127.0.0.1:7860
python3代码中使用
import subprocess# 调用 mineru 命令行解析文档
result = subprocess.run(["mineru", "-p", "test.pdf", "-o", "output", "-b", "pipeline", "-d", "cpu"],capture_output=True,text=True
)
print(result.stdout) # 输出解析过程日志、结果信息