import fitz # PyMuPDF库 import os # 读取PDF文件的内容 def read_pdf(file_path): doc = fitz.open(file_path) content = "" for page in doc: content += page.getText("text") doc.close() return content # 去除字符串中的空格和空行 def remove_spaces(text): return "\n".join([line.strip() for line in text.split("\n") if line.strip()]) # 将字符串按中文句号分割成多个语句 def split_sentences(text): sentences = [] for sentence in text.split("。"): sentence = sentence.strip() if sentence: sentences.append(sentence + "。") return sentences # 读取指定目录下所有PDF文件的内容并合并成一个字符串 def read_all_pdfs(dir_path): all_content = "" for file_name in os.listdir(dir_path): if file_name.endswith(".pdf"): file_path = os.path.join(dir_path, file_name) content = read_pdf(file_path) content = remove_spaces(content) all_content += content return all_content # 将字符串按中文句号分割成多个语句并打印出来 def print_sentences(text): sentences = split_sentences(text) for sentence in sentences: print(sentence) # 测试 dir_path = r"D:\点宽学院" all_content = read_all_pdfs(dir_path) print_sentences(all_content)这是我的代码显示Traceback (most recent call last): File "D:\python+pycharm\ceshi.py", line 44, in <module> all_content = read_all_pdfs(dir_path) File "D:\python+pycharm\ceshi.py", line 31, in read_all_pdfs content = read_pdf(file_path) File "D:\python+pycharm\ceshi.py", line 8, in read_pdf content += page.getText("text") AttributeError: 'Page' object has no attribute 'getText',如何修改呢
时间: 2023-06-19 12:03:12 浏览: 177
这个错误提示说明 `Page` 对象没有 `getText` 方法。可能是因为你使用的 PyMuPDF 版本不同于示例代码中使用的版本。建议尝试以下两种方法:
1. 使用 `extract_text()` 方法代替 `getText()` 方法,例如:
```python
content += page.extract_text()
```
2. 使用 PyMuPDF 最新版本(1.18.8),因为该版本支持 `getText()` 方法。可以通过以下命令安装最新版本:
```python
pip install PyMuPDF==1.18.8
```
希望能够帮到你!
相关问题
import fitz # PyMuPDF库 import os # 读取PDF文件的内容 def read_pdf(file_path): doc = fitz.open(file_path) content = "" for page in doc: content += page.getText("text") doc.close() return content # 去除字符串中的空格和空行 def remove_spaces(text): return "\n".join([line.strip() for line in text.split("\n") if line.strip()]) # 将字符串按中文句号分割成多个语句 def split_sentences(text): sentences = [] for sentence in text.split("。"): sentence = sentence.strip() if sentence: sentences.append(sentence + "。") return sentences # 读取指定目录下所有PDF文件的内容并合并成一个字符串 def read_all_pdfs(dir_path): all_content = "" for file_name in os.listdir(dir_path): if file_name.endswith(".pdf"): file_path = os.path.join(dir_path, file_name) content = read_pdf(file_path) content = remove_spaces(content) all_content += content return all_content # 将字符串按中文句号分割成多个语句并打印出来 def print_sentences(text): sentences = split_sentences(text) for sentence in sentences: print(sentence) # 测试 dir_path = r"D:\点宽学院" all_content = read_all_pdfs(dir_path) print_sentences(all_content)代码运行显示AttributeError: 'Page' object has no attribute 'getText'我该如何修改
这个错误可能是因为fitz的版本问题,可以尝试更新fitz库。同时,建议在读取PDF文件时,使用try-except语句来捕捉异常,以便更好地处理错误。
以下是更新fitz库和添加try-except语句的修改代码:
```python
import fitz # PyMuPDF库
import os # 读取PDF文件的内容
def read_pdf(file_path):
doc = fitz.open(file_path)
content = ""
for page in doc:
try:
content += page.getText("text")
except:
pass
doc.close()
return content
# 去除字符串中的空格和空行
def remove_spaces(text):
return "\n".join([line.strip() for line in text.split("\n") if line.strip()])
# 将字符串按中文句号分割成多个语句
def split_sentences(text):
sentences = []
for sentence in text.split("。"):
sentence = sentence.strip()
if sentence:
sentences.append(sentence + "。")
return sentences
# 读取指定目录下所有PDF文件的内容并合并成一个字符串
def read_all_pdfs(dir_path):
all_content = ""
for file_name in os.listdir(dir_path):
if file_name.endswith(".pdf"):
file_path = os.path.join(dir_path, file_name)
content = read_pdf(file_path)
content = remove_spaces(content)
all_content += content
return all_content
# 将字符串按中文句号分割成多个语句并打印出来
def print_sentences(text):
sentences = split_sentences(text)
for sentence in sentences:
print(sentence)
# 测试
dir_path = r"D:\点宽学院"
all_content = read_all_pdfs(dir_path)
print_sentences(all_content)
```
我想读取“D:\点宽学院”下所有PDF内容,将所有读取到的内容合并成一个字符串,去掉其中所有空行与空格。然后用中文句号进行分割,一句话中含有一个句号,把分割出来的语句一一打印出来,请用python实现,不能用pypdf2和pdfminer库
可以使用PyMuPDF库来实现这个功能。以下是代码实现:
```python
import fitz # PyMuPDF库
# 读取PDF文件的内容
def read_pdf(file_path):
doc = fitz.open(file_path)
content = ""
for page in doc:
content += page.getText()
doc.close()
return content
# 去除字符串中的空格和空行
def remove_spaces(text):
return "\n".join([line.strip() for line in text.split("\n") if line.strip()])
# 将字符串按中文句号分割成多个语句
def split_sentences(text):
sentences = []
for sentence in text.split("。"):
sentence = sentence.strip()
if sentence:
sentences.append(sentence + "。")
return sentences
# 读取指定目录下所有PDF文件的内容并合并成一个字符串
def read_all_pdfs(dir_path):
all_content = ""
for file_name in os.listdir(dir_path):
if file_name.endswith(".pdf"):
file_path = os.path.join(dir_path, file_name)
content = read_pdf(file_path)
content = remove_spaces(content)
all_content += content
return all_content
# 将字符串按中文句号分割成多个语句并打印出来
def print_sentences(text):
sentences = split_sentences(text)
for sentence in sentences:
print(sentence)
# 测试
dir_path = r"D:\点宽学院"
all_content = read_all_pdfs(dir_path)
print_sentences(all_content)
```
这段代码会读取指定目录下所有PDF文件的内容,并将它们合并成一个字符串。然后会去掉其中的空格和空行,并按中文句号分割成多个语句。最后将分割出来的语句逐一打印出来。
阅读全文