资讯动态

ai解析文档帮助工具1

发布时间:2026/9/22 2:51:12 来源:尧图企业网站定制
在我们使用ai的时候大概率会遇到各种各样的文件想要让ai去识别解析然后给我们提供解读分析但是总会有识别失败或者错误的问题下面讲一个微软开源的万能格式转换器。PDF、Word、Excel、PPT、图片、音频、HTML、甚至 YouTube 视频它都能一把梭转成 Markdown。开源地址它本质上就是一个python脚本。1.想要使用它就需要先安装一个py环境官网链接安装完成后pip--version这个指令可以查看版本。python-mpipinstall--upgradepip这个指令可以升级版本。2.使用pip 安装markitdown首先使用国内镜像pip configsetglobal.index-urlhttps://pypi.tuna.tsinghua.edu.cn/simple执行下载pip install markitdown[all]3.使用 markitdown转换文件markitdownC:\Users\kpl\Desktop\example.pdfoutput.md或者自己创建一个py脚本也可以。frommarkitdown importMarkItDownmdMarkItDown()resultmd.convert(rC:\Users\kpl\Desktop\test.docx)print(result.text_content)再或者让ai写一个脚本哈哈可以交互式执行转换操作的# # MarkItDown 万能格式转换器 - Streamlit 交互界面# 文档转换: pip install markitdown streamlit# 图片OCR: pipinstalleasyocr(首次运行会自动下载模型)# 使用: streamlit run markitdown_gui.py# import os import tempfile import streamlitasst# ── 页面配置 ──────────────────────────────────────────────st.set_page_config(page_titleMarkItDown 万能格式转换器,page_icon,layoutcentered,menu_items{about:Powered by MarkItDown (Microsoft) EasyOCR})# ── 自定义样式 ──────────────────────────────────────────────st.markdown(style.stApp{background:#f0f4ff;}.main-card{background:white;border-radius:18px;padding:36px;box-shadow:06px 32pxrgba(0,0,0,0.09);}.success-msg{background:#d4edda;border:1px solid #c3e6cb;border-radius:8px;padding:10px 18px;color:#155724;font-weight:600;margin-bottom:12px;}.format-chip{display:inline-block;background:#e8f0fe;color:#1a73e8;border-radius:6px;padding:2px 10px;margin:2px;font-size:13px;}.ocr-badge{display:inline-block;background:#fff3e0;color:#e65100;border-radius:6px;padding:2px 10px;margin:2px;font-size:13px;}h1{color:#1a1a2e;font-weight:800;}.stButtonbutton{border-radius:10px;font-weight:700;}/style,unsafe_allow_htmlTrue)# ── 标题 ───────────────────────────────────────────────────st.title( MarkItDown 万能格式转换器)st.markdown(微软开源文档转换器 **EasyOCR 图片文字识别**支持格式全覆盖)st.markdown(---)# ── 格式说明 ───────────────────────────────────────────────st.markdown(** 文档格式**用 MarkItDown 转换)doc_formats[PDF,Word(docx/doc),Excel(xlsx/xls),PPT(pptx/ppt),CSV,HTML,Markdown]st.markdown( .join(fspanclassformat-chip{f}/spanforfindoc_formats),unsafe_allow_htmlTrue)st.markdown(**️ 图片格式**用 EasyOCR 识别文字)img_formats[PNG,JPG / JPEG,带文字的截图、照片、扫描件]st.markdown( .join(fspanclassocr-badge{f}/spanforfinimg_formats),unsafe_allow_htmlTrue)st.markdown()# ── 文件上传 ─────────────────────────────────────────────────ALLOWED_EXTS[pdf,docx,doc,xlsx,xls,pptx,ppt,csv,html,htm,md,png,jpg,jpeg,gif]uploaded_filest.file_uploader( 点击选择文件 或 拖拽文件到此处,typeALLOWED_EXTS,help支持 PDF、Word、Excel、PowerPoint、CSV、HTML、图片等)# ── 核心变量 ─────────────────────────────────────────────────result_contentNoneresult_filenameNone# ── 转换逻辑 ─────────────────────────────────────────────────ifuploaded_fileisnotNone:file_extos.path.splitext(uploaded_file.name)[1].lower().lstrip(.)is_imagefile_extin(png,jpg,jpeg,gif)col_info,col_sizest.columns([3,1])withcol_info:st.success(f {uploaded_file.name})withcol_size:st.info(f{(uploaded_file.size / 1024):.1f} KB)# ── 保存临时文件 ──────────────────────────────────────────suffix.file_extwithtempfile.NamedTemporaryFile(deleteFalse,suffixsuffix)astmp:tmp.write(uploaded_file.getbuffer())tmp_pathtmp.name# ══════════════════════════════════════════════════════════# 方式一图片 → EasyOCR 识别# ══════════════════════════════════════════════════════════ifis_image:withst.spinner( 正在识别图片文字首次使用需下载模型请耐心等待...):try:import easyocr st.cache_resourcedefget_ocr_reader():# 中英文模型首次运行自动下载returneasyocr.Reader([ch_sim,en],gpuFalse,verboseFalse)readerget_ocr_reader()ocr_resultsreader.readtext(tmp_path)ifnotocr_results:st.warning(⚠️ 未在图片中检测到文字可能是纯图片或文字太模糊。)else:lines[]forbbox,text,confidenceinocr_results:ifconfidence0.3andtext.strip():lines.append(text.strip())result_content\n.join(lines)result_filenameos.path.splitext(uploaded_file.name)[0]_OCR.txtst.markdown(fdivclasssuccess-msg✅ 识别完成共提取{len(result_content)}个字符 f{len([xforxinocr_resultsiffloat(x[2])0.3])}个文本块/div,unsafe_allow_htmlTrue)st.text_area(识别结果,valueresult_content,height350,label_visibilitycollapsed,keyocr_result)except Exceptionase:st.error(f❌ OCR 识别失败{e})finally:os.unlink(tmp_path)# ══════════════════════════════════════════════════════════# 方式二文档 → MarkItDown 转换# ══════════════════════════════════════════════════════════else:ifst.button( 开始转换,typeprimary):withst.spinner(正在转换请稍候…):try:frommarkitdown importMarkItDownmdMarkItDown()resultmd.convert(tmp_path)result_contentresult.text_content.strip()ifnotresult_content:st.warning(⚠️ 转换结果为空可能是文件为空或格式不支持。)else:result_filenameos.path.splitext(uploaded_file.name)[0].mdst.markdown(fdivclasssuccess-msg✅ 转换成功共{len(result_content)}个字符/div,unsafe_allow_htmlTrue)# 预览st.text_area(转换结果Markdown,valueresult_content,height400,label_visibilitycollapsed,keymd_result)except Exceptionase:st.error(f❌ 转换失败{e})finally:os.unlink(tmp_path)# ── 下载按钮 ──────────────────────────────────────────────ifresult_content:st.markdown(---)st.download_button( 下载结果文件,dataresult_content.encode(utf-8),file_nameresult_filename,mimetext/plain;charsetutf-8,typeprimary)# ── 页脚 ─────────────────────────────────────────────────────st.markdown(---)st.caption(MarkItDown (Microsoft) EasyOCR · Built with Streamlit)执行方式运行cmd后streamlitrunC:\Users\kpl\Desktop\markitdown_gui.py运行后的界面就是这个样子。注意这个脚本使用了easyOcr来识别图片因为MarkItDown 转图片根本不是用 OCR而是 1. 提取图片的 EXIF 元数据需要装 exiftool 2.调用多模态 AI如 GPT-4V来描述图片。文件转换为md文档后再交给ai去识别解析做一些东西就方便多了。

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价