搜尋
首頁後端開發Python教學如何使用 PyMuPDFM 將 PDF 轉換為 Markdown 及其評估

PyMuPDF4LLM 是一個設計用來將 PDF 轉換為 Markdown 格式的函式庫。在這裡,我將分享我測試這個庫的經驗。

安裝

先使用下列指令安裝函式庫:


pip install pymupdf4llm


用法

基本用法非常簡單,只需三行程式碼即可將 PDF 轉換為 Markdown:


import pymupdf4llm
md_text = pymupdf4llm.to_markdown("input.pdf")
print(md_text)


您可以指定參數來調整內容的擷取方式。

按頁提取文本

預設情況下,整個 PDF 會轉換為單一文字輸出。但是,您可以透過指定 page_chunks=True 逐頁提取文字。


md_text = pymupdf4llm.to_markdown("input.pdf", page_chunks=True)


擷取影像

要將映像提取為文件,請使用 write_images=True 選項:


md_text = pymupdf4llm.to_markdown("input.pdf", write_images=True)


也可以使用base64編碼直接在Markdown嵌入圖片:


md_text = pymupdf4llm.to_markdown("input.pdf", embed_images=True)


轉換結果評估

為了進行測試,使用了各種具有不同 Markdown 元素的 PDF。

How to Convert PDFs to Markdown Using PyMuPDFM and Its Evaluation

標頭轉換

標題已正確轉換為 Markdown 格式。這是結果的一部分:


# Sample Markdown Guide

This is a sample markdown file that includes various features for quick reference.

## 1. Headers

...

## 3. Lists


粗體和斜體文本

粗體和斜體格式也已正確轉換:


**Bold: **Bold Text****

_Italic: *Italic Text*_

**_Bold and Italic: ***Bold and Italic***_**


清單轉換

第一層有序列表轉換沒有問題,但巢狀列表和無序列表轉換不準確。

How to Convert PDFs to Markdown Using PyMuPDFM and Its Evaluation


## 3. Lists

### Unordered List

Item 1

Item 2

Sub-item 1

Sub-item 2

### Ordered List

1. First item

2. Second item

1. Sub-item A

2. Sub-item B


連結轉換

提取了鏈接的URL,但包含鏈接的整行變成了超鏈接,偏離了原始格式。

How to Convert PDFs to Markdown Using PyMuPDFM and Its Evaluation


## 4. Links and Images

[You can add links using [Link Text](URL).](https://www.example.com/)


影像擷取

預設不會提取映像,但可以使用 write_images=True 將映像保存在本地。


md_text = pymupdf4llm.to_markdown("input.pdf", write_images=True)


然後在 Markdown 中引用已儲存的圖片,如下所示:


<p>### Image Example</p>

<p>![](input.pdf-1-0.png)</p>




表轉換

沒有垂直邊框的簡單表格無法準確轉換(可能是因為不明確的列邊界導致表格被視為純文字)。

How to Convert PDFs to Markdown Using PyMuPDFM and Its Evaluation


<p>## 5. Tables</p>

<p>**Column 1** **Column 2** **Column 3**</p>

<p>Row 1 Data A Data B</p>

<p>Row 2 Data C Data D</p>




代碼轉換

程式碼區塊已正確轉換,但語言規範(例如 python)未保留。內聯代碼轉換也存在問題。

How to Convert PDFs to Markdown Using PyMuPDFM and Its Evaluation


<p>## 6. Code</p>

<p>### Inline Code</p>

<p>Use backticks for inline code: print("Hello, world!")</p>

<p>### Code Block</p>

<p>Use triple backticks for code blocks:</p>

<p>```<br>
def greet(name):<br>
  return f"Hello, {name}!"<br>
print(greet("Markdown"))<br>
```</p>




多行文字

對於多行文本,換行符號將按照原始 PDF 中的顯示方式保留。

How to Convert PDFs to Markdown Using PyMuPDFM and Its Evaluation


<p>Markdown is a lightweight and versatile markup language favored by developers, writers, and bloggers alike</p>

<p>due to its simplicity in formatting text, enabling users to create readable and well-structured documents—</p>

<p>whether for documentation, blog posts, or articles—without the complexity of HTML, while also offering the</p>

<p>ability to convert content seamlessly into other formats like HTML, PDF, and even slideshows, making it an</p>

<p>ideal choice for projects that require both clarity and flexibility in presentation.</p>




結論

儘管在準確轉換清單和連結方面存在挑戰,PyMuPDF4LLM 是將 PDF 轉換為 Markdown 的有用工具。它可以在本地工作,無需外部語言模型,適合無法訪問互聯網的環境。

以上是如何使用 PyMuPDFM 將 PDF 轉換為 Markdown 及其評估的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何將元素附加到Python列表中?您如何將元素附加到Python列表中?May 04, 2025 am 12:17 AM

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

您如何創建Python列表?舉一個例子。您如何創建Python列表?舉一個例子。May 04, 2025 am 12:16 AM

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

討論有效存儲和數值數據的處理至關重要的實際用例。討論有效存儲和數值數據的處理至關重要的實際用例。May 04, 2025 am 12:11 AM

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。

您如何創建Python數組?舉一個例子。您如何創建Python數組?舉一個例子。May 04, 2025 am 12:10 AM

pythonarraysarecreatedusiseThearrayModule,notbuilt-Inlikelists.1)importThearrayModule.2)指定tefifythetypecode,例如,'i'forineizewithvalues.arreaysofferbettermemoremorefferbettermemoryfforhomogeNogeNogeNogeNogeNogeNogeNATATABUTESFELLESSFRESSIFERSTEMIFICETISTHANANLISTS。

使用Shebang系列指定Python解釋器有哪些替代方法?使用Shebang系列指定Python解釋器有哪些替代方法?May 04, 2025 am 12:07 AM

除了shebang線,還有多種方法可以指定Python解釋器:1.直接使用命令行中的python命令;2.使用批處理文件或shell腳本;3.使用構建工具如Make或CMake;4.使用任務運行器如Invoke。每個方法都有其優缺點,選擇適合項目需求的方法很重要。

列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版