简体   繁体   English

Python OS For Loop - TypeError:“模块”对象不可调用

[英]Python OS For Loop - TypeError: 'module' object is not callable

I have the following code to iterate over each file in a folder and run the pdfplumber module on it:我有以下代码来遍历文件夹中的每个文件并在其上运行pdfplumber模块:

#Import required packages:
import os
import pdfplumber

#Iterate over pdf's 
os.chdir(r'C:\MyDocuments\PDF')
directory = r'C:\MyDocuments\PDF'
for filename in os.listdir(directory):                              #iterate over each file in directory
    if filename.endswith(".pdf"):                                   #if the file is a pdf (we will be creating .txt files - do not want to include these)
        pdf = pdfplumber(filename)                                  #pdfplumber extracts the pdf file first
    else:
        continue

There is more going on once the PDF is imported with pdfplumber , but this for loop gives the gist of the issue.一旦使用pdfplumber导入 PDF,还有更多事情pdfplumber ,但是这个for循环给出了问题的要点。 When I run this, I get the error message当我运行它时,我收到错误消息

TypeError: 'module' object is not callable

It seems that there should be a solution in something like: from os import ... where ... should be whatever function is needed (I think).似乎应该有一个解决方案,例如: from os import ... where ... 应该是需要的任何功能(我认为)。 If this is a solution though, I don't know which specific function I would need to import from os.如果这是一个解决方案,我不知道我需要从 os. If someone would have a suggestion on how to make this work, I would greatly appreciate it!如果有人对如何使这项工作有建议,我将不胜感激!

pdfplumber is itself the class name. pdfplumber本身就是类名。 You need to use .open() for this:您需要为此使用.open()

import os
import pdfplumber

#Iterate over pdf's 
os.chdir(r'C:\MyDocuments\PDF')
directory = r'C:\MyDocuments\PDF'
for filename in os.listdir(directory):                              #iterate over each file in directory
    if filename.endswith(".pdf"):                                   #if the file is a pdf (we will be creating .txt files - do not want to include these)
        pdf = pdfplumber.open(filename)                                  #pdfplumber extracts the pdf file first
    else:
        continue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM