我创建了一个函数,该函数将打开目录中的每个文件,并从每个文件中提取文本,然后使用Pandas将其输出到excel工作表中。每种文件类型的索引似乎都可以正常工作。但是,当从路径目录中的第一个文件中提取文本时,似乎是用第一个文件的提取文本替换了其他文件中的其他提取文本。请帮忙,谢谢!
from pathlib import Path
import shutil
from datetime import datetime
import time
from configparser import ConfigParser
import glob
import fileinput
import pandas as pd
import os
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
import docx2txt
from pptx import Presentation
p = Path('C:/Users/XXXX/Desktop/test_folder')
txt_files = list(p.rglob('*txt'))
PDF_files = list(p.rglob('*pdf'))
csv_files = list(p.rglob('*csv'))
docx_files = list(p.rglob('*docx'))
pptx_files = list(p.rglob('*pptx'))
def loader(path):
with open(str(path.resolve()),"r",encoding = "ISO-8859-1") as f:
docx_out,pptx_out = [],[]
data = []
print(pptx_files)
if path.suffix == ".pdf":
for name1 in PDF_files:
data.append(pdf_to_text(name1))
return data
elif path.suffix == ".docx":
for name2 in docx_files:
docx_out = (docx2txt.process(name2))
return docx_out
elif path.suffix == ".pptx":
for file in pptx_files:
prs = Presentation(file)
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
pptx_out.append(run.text)
return pptx_out
else:
return f.readlines()
文本内容文件名 这是一个测试first_pdf.pdf
答案 0 :(得分:1)
此街区
if path.suffix == ".pdf":
for name1 in PDF_files:
data.append(pdf_to_text(name1))
return data
在附加第一个PDF文件后从函数返回。它永远不会到达第二个,因为您是从for
循环内部返回的。这应该解决它:
if path.suffix == ".pdf":
for name1 in PDF_files:
data.append(pdf_to_text(name1))
return data