【问题标题】:PDF scraping: how to automate the creation of txt files for each pdf scraped in Python?PDF 抓取:如何为 Python 中抓取的每个 pdf 自动创建 txt 文件?
【发布时间】:2015-04-18 18:32:14
【问题描述】:

这就是我想要做的:一个程序,它将一个 pdf 文件列表作为其输入,并为列表中的每个文件返回一个 .txt 文件。

例如,给定一个 listA = ["file1.pdf", "file2.pdf", "file3.pdf"],我希望 Python 创建三个 txt 文件(每个 pdf 文件一个),比​​如“file1.pdf”。 txt”、“file2.txt”和“file3.txt”。

感谢this guy,我的转换部分工作顺利。我所做的唯一更改是在 maxpages 语句中,我在其中分配了 1 而不是 0,以便仅提取第一页。正如我所说,我的这部分代码运行良好。这是代码。

def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
#maxpages = 0
maxpages = 1
caching = True
pagenos=set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
    interpreter.process_page(page)
fp.close()
device.close()
str = retstr.getvalue()
retstr.close()
return str

问题是我似乎无法让 Python 返回我,这就是我在第二段中所说的。我试过以下代码:

def save(lst):
i = 0

while i < len(lst):
    txtfile = "enegep"+str(i)+".txt" #enegep is like the identifier of the files
    artigo = convert_pdf_to_txt(lst[0])
    with open(txtfile, "w") as textfile:
        textfile.write(artigo)
    i += 1

我使用包含两个 pdf 文件的列表作为输入运行了该保存函数,但它只生成了一个 txt 文件,并且运行了几分钟而没有生成第二个 txt 文件。实现目标的更好方法是什么?

【问题讨论】:

    标签: python loops pdf pdfminer


    【解决方案1】:

    您不更新i,因此您的代码陷入无限循环,您需要i += 1

    def save(lst):
        i = 0   # set to 0 but never changes
        while i < len(lst):
            txtfile = "enegep"+str(i)+".txt" #enegep is like the identifier of the files
            artigo = convert_pdf_to_txt(lista[0])
            with open(txtfile, "w") as textfile:
                textfile.write(artigo)
         i += 1 # you need to  increment i
    

    更好的选择是简单地使用range

    def save(lst):
        for i in range(len(lst)): 
            txtfile = "enegep{}.txt".format(i) #enegep is like the identifier of the files
            artigo = convert_pdf_to_txt(lista[0])
            with open(txtfile, "w") as textfile:
                textfile.write(artigo)
    

    您也只使用lista[0],因此您可能还想更改该代码以在每次迭代时在列表中移动。

    如果 lst 实际上是 lista 你可以使用enumerate:

       def save(lst):
            for i, ele in enumerate(lst): 
                txtfile = "enegep{}.txt".format(i) #enegep is like the identifier of the files
                artigo = convert_pdf_to_txt(ele)
                with open(txtfile, "w") as textfile:
                    textfile.write(artigo)
    

    【讨论】:

    • 很抱歉,在将代码发布到这里之前,我没有意识到我的代码有一些拼写错误和小错误。我刚刚修好了它们。顺便说一句,“lista”在葡萄牙语中的意思是列表。编辑:第二个效果很好,非常感谢。
    • @iatowks,你仍然需要使用超过 lista[0],你确定你有 i += 1 在正确的地方吗?试试我提供的最后一个代码
    • 我使用了您在我的代码中编写的第三个选项,它给了我预期的结果。再次,非常感谢。
    • 没有问题。很高兴它有帮助
    猜你喜欢
    • 2021-09-18
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2019-07-22
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    相关资源
    最近更新 更多