Skip to content

Latest commit

 

History

History
193 lines (118 loc) · 6.43 KB

格式转换markdown调研报告.md

File metadata and controls

193 lines (118 loc) · 6.43 KB

格式转换markdown调研报告

时间: 2025年3月13日

下文主要介绍doc和pdf文件格式转换成md得技术实现,技术栈语言为golang。另外目前golang有一个docx解析库可以使用 ,虽然不完美,但是可以解决md处理问题。

技术路线主要有4种方式,优先级从高到底

  • 寻找高star免费开源库
  • 使用程序包装命令行,包装工具调用
  • 采用大模型技术
  • 服务供应商

另外还有一种方案,如果实在不能转换成md则,直接提取文本,不带格式文本得方式。

一、pdf转换成md技术线路

曲线路径方法

  • pdf转成html html转成md
  • pdf转成docx docx转成md

pdf转md库

pdf转docx库

二、doc技术线路

  • doc转成html html转成md
  • doc转成docx docx转成md

doc转html

  • 使用LibreOffice进行转换

转换后可使用 https://github.com/JohannesKaufmann/html-to-markdown 进行成md

doc转docx

  • 使用LibreOffice进行转换

可配合 https://github.com/qifengzhang007/gooxml 进行转换

三、采用大模型

四、收费服务

其他

以下罗列出可能有用,或者比较有意思的库


LibreOffice使用

libreoffice是一个免费切开源的产品,支持多种文件格式相互之间转换。

https://zh-cn.libreoffice.org/

soffice命令行使用

提示: soffice为命令行工具,同时也是个gui程序,根据启动参数--headless来区别,另外可能收到不同系统或版本的影响有的叫 libreoffcie

pdf -> docx

soffice --headless --infilter="writer_pdf_import" --convert-to docx  ../testdata/test.pdf --outdir ../testdata/

提示:如果出现Error: source file could not be loaded 则没有指定--outdir参数

pdf -> html

soffice --headless  --convert-to html ./testdata/test.pdf --outdir ../testdata/ --infilter=writer_pdf_import

doc -> txt

soffice --convert-to txt ./testdata/test.doc ./testdata/testdoc.txt

doc - >docx

soffice --headless --convert-to docx  ../../doc/testdata/test.doc --outdir ../testdata/

注意这个命令行有问题,调用直接阻塞住了服务不知道为何?

soffice --headless  ./testdata/test.pdf ./testdata/testpdf.txt --infilter=writer_pdf_import

unoconvert命令使用

unoconvert是一个python项目,利用python包装了libreoffice服务,支持多种文件转换

https://github.com/unoconv/unoserver

pdf -> docx

/Users/edy/Library/Python/3.9/bin/unoconvert --verbose --host=127.0.0.1 --port=2003 --host-location=remote --convert-to=docx ./testdata/test.pdf ./testdata/testpdf.docx --input-filter=writer_pdf_import

pdf -> txt

 /Users/edy/Library/Python/3.9/bin/unoconvert --verbose --host=127.0.0.1 --port=2003 --host-location=remote --convert-to=txt ./testdata/test.pdf ./testdata/testtext.txt --input-filter=writer_pdf_import

提示:不知为啥为空转换完

pdf -> html

/Users/edy/Library/Python/3.9/bin/unoconvert --verbose --host=127.0.0.1 --port=2003 --host-location=remote --convert-to="html" ./testdata/test.pdf ./testdata/testpdf.html --input-filter=writer_pdf_import

提示: 不知为何转换出来的内容为空,但是html中有样式每有内容

/Users/edy/Library/Python/3.9/bin/unoconvert --verbose --host=127.0.0.1 --port=2003 --host-location=remote --convert-to=docx ./testdata/test.doc ./testdata/testdoc.docx

libreoffice 使用相关问题

  1. 使用 unoconvert 或者其他包装库,或代码调用,服务调用阻塞hang住了,原因是没有启动unoserver服务( 这个问题我卡住了1天特此记一下)
  2. 使用unoconvert 报告文件not exist 错误,如果本地文件确实存在,那没有可能是unoconvert服务启动连接方式不对也就是 --host-location=local如果unoconvertunoserver不在同一个机器上,那么--host-location=remote不在一太机器内则,改为 --host-location=remote

参考项目

值得参考libreoffice镜像

如果想把自己的代码服务和libreoffice服务构建在一个镜像中,可以参考以下镜像


最终方案

doc

doc -(libreoffice)-> docx -> 调用 https://github.com/qifengzhang007/gooxml 库自己构建md

pdf

方案一

pdf -(libreoffice)-> html -> htmltomd(golang库)

注意:该方案目前存在一个问题,unoserver转换有问题,使用soffice命令转换是没有问题的,但是unoserver就有问题。 等待issues反馈 unoconv/unoserver#164

方案二

兜底方案

使用gen2brain 提取text

或使用ledongthuc 提取文本