import fitz # PyMuPDF库 # 打开PDF文件 doc = fitz.open(r"D:\点宽学院\在线绿色互动如何影响共享型绿色消费行为_——自然联结性的调节作用_王建明.pdf") text = "" # 存储PDF内容的字符串 for page in doc: text += page.getText() # 将文本按照中文句号分割成句子 sentences = text.split("。") # 打印每一句 for sentence in sentences: print(sentence.strip() + "。")我的代码报错,显示AttributeError: 'Page' object has no attribute 'getText'我该如何修改
时间: 2023-06-19 07:04:13 浏览: 51
你可以将`page.getText()` 改为 `page.get_text()`, 也就是将 `getText()` 方法改为 `get_text()` 方法,即:
```
import fitz
# 打开PDF文件
doc = fitz.open(r"D:\点宽学院\在线绿色互动如何影响共享型绿色消费行为_——自然联结性的调节作用_王建明.pdf")
text = "" # 存储PDF内容的字符串
for page in doc:
text += page.get_text() # 将文本按照中文句号分割成句子
sentences = text.split("。") # 打印每一句
for sentence in sentences:
print(sentence.strip() + "。")
```
这样应该就能够正确地获取PDF中的文本并分割成句子了。
相关问题
import fitz # 打开PDF文件 doc = fitz.open(r"D:\点宽学院\在线绿色互动如何影响共享型绿色消费行为_——自然联结性的调节作用_王建明.pdf") text = "" # 存储PDF内容的字符串 for page in doc: text += page.get_text() # 将文本按照中文句号分割成句子 sentences = text.split("。") # 打印每一句 for sentence in sentences: print(sentence.strip() + "。")请帮我修改这个代码,我想把处理的对象改为“D:\点宽学院”下所有的PDF文件
可以使用Python的os模块来获取指定目录下的所有PDF文件路径,然后对每个PDF文件进行处理。
以下是修改后的代码:
```python
import os
import fitz
# 获取指定目录下的所有PDF文件路径
pdf_dir = r"D:\点宽学院"
pdf_files = [os.path.join(pdf_dir, f) for f in os.listdir(pdf_dir) if f.endswith('.pdf')]
for pdf_file in pdf_files:
# 打开PDF文件
doc = fitz.open(pdf_file)
text = "" # 存储PDF内容的字符串
for page in doc:
text += page.get_text()
# 将文本按照中文句号分割成句子
sentences = text.split("。")
# 打印每一句
for sentence in sentences:
print(sentence.strip() + "。")
```
这样就可以处理指定目录下所有的PDF文件了。
import os import fitz # 获取指定目录下的所有PDF文件路径 pdf_dir = r"D:\点宽学院" pdf_files = [os.path.join(pdf_dir, f) for f in os.listdir(pdf_dir) if f.endswith('.pdf')] for pdf_file in pdf_files: # 打开PDF文件 doc = fitz.open(pdf_file) text = "" # 存储PDF内容的字符串 for page in doc: text += page.get_text() # 将文本按照中文句号分割成句子 sentences = text.split("。") # 打印每一句 for sentence in sentences: print(sentence.strip() + "。")这是我的代码,需求发生变更,合并pdf内容后要先把所有的空行和空格去掉再分割,请帮我修改
import os
import fitz
# 获取指定目录下的所有PDF文件路径
pdf_dir = r"D:\点宽学院"
pdf_files = [os.path.join(pdf_dir, f) for f in os.listdir(pdf_dir) if f.endswith('.pdf')]
for pdf_file in pdf_files:
# 打开PDF文件
doc = fitz.open(pdf_file)
text = ""
# 存储PDF内容的字符串
for page in doc:
text += page.get_text()
# 去掉所有空格和空行
text = text.replace(" ", "").replace("\n", "")
# 将文本按照中文句号分割成句子
sentences = text.split("。")
# 打印每一句
for sentence in sentences:
print(sentence.strip() + "。")
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)