python 操作pdf
有勇气的牛排
233
Python
2023-05-18 20:47:02
导入包
import pdfplumber
函数说明
函数 |
说明 |
extract_text() |
读取文字 |
extract_words() |
|
extract_table() |
读取表格(列表) |
extract_tables() |
读取表格 |
读取pdf指定页面内容
with pdfplumber.open('C:/Users/Administrator/Desktop/期末/0操作系统/操作系统习题1.pdf') as pdf:
first_page = pdf.pages[0]
print(first_page.extract_text())
读取所有页面内容
with pdfplumber.open('C:/Users/Administrator/Desktop/期末/0操作系统/操作系统习题1.pdf') as pdf:
for page in pdf.pages:
print(page.extract_text())
读取表格

参考:
https://www.bilibili.com/video/BV15E411g7py?p=2
<h2><a id="_1"></a>导入包</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">import</span> pdfplumber
</code></div></pre>
<h2><a id="_7"></a>函数说明</h2>
<table>
<thead>
<tr>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>extract_text()</td>
<td>读取文字</td>
</tr>
<tr>
<td>extract_words()</td>
<td></td>
</tr>
<tr>
<td>extract_table()</td>
<td>读取表格(列表)</td>
</tr>
<tr>
<td>extract_tables()</td>
<td>读取表格</td>
</tr>
</tbody>
</table>
<h2><a id="pdf_15"></a>读取pdf指定页面内容</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">with</span> pdfplumber.<span class="hljs-built_in">open</span>(<span class="hljs-string">'C:/Users/Administrator/Desktop/期末/0操作系统/操作系统习题1.pdf'</span>) <span class="hljs-keyword">as</span> pdf:
first_page = pdf.pages[<span class="hljs-number">0</span>] <span class="hljs-comment"># 第一页</span>
<span class="hljs-built_in">print</span>(first_page.extract_text())
</code></div></pre>
<h2><a id="_22"></a>读取所有页面内容</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">with</span> pdfplumber.<span class="hljs-built_in">open</span>(<span class="hljs-string">'C:/Users/Administrator/Desktop/期末/0操作系统/操作系统习题1.pdf'</span>) <span class="hljs-keyword">as</span> pdf:
<span class="hljs-keyword">for</span> page <span class="hljs-keyword">in</span> pdf.pages:
<span class="hljs-built_in">print</span>(page.extract_text())
</code></div></pre>
<h2><a id="_30"></a>读取表格</h2>
<p><img src="https://static.couragesteak.com/article/777116d3fcc841618f3b8a87e1c4c4df.png" alt="image.png" /></p>
<p>参考:<br />
https://www.bilibili.com/video/BV15E411g7py?p=2</p>
留言