gpt4 book ai didi

python - Forloop用于将目录中的所有pdf转换为excel文件不起作用 - python

转载 作者:行者123 更新时间:2023-12-04 20:51:29 25 4
gpt4 key购买 nike

我正在尝试将文件夹中的所有 pdf 转换为 excel 文件。为此,我正在使用以下代码,尽管我收到以下错误:

FileNotFoundError: [Errno 2] No such file or directory: 'filepath.pdf'

这是非功能代码:
# import packages needed
import glob
!pip install tabula-py
import tabula

# set up working directory
my_dir = 'C:/Users/myfolderwithpdfs'

# transform the pdfs into excel files
for filepath in glob.iglob('my_dir/*.pdf'):
tabula.convert_into("filepath.pdf","filepath.xlsx", output_format="xlsx")

当我只使用 for 循环来打印我的文件列表时(如下)
for filepath in glob.iglob('my_dir/*.pdf'):
print(filepath)

或转换单个文件
tabula.convert_into("myfilename.pdf", "myfilename.xlsx", output_format="xlsx")

我的代码没有遇到任何问题或错误。

最佳答案

您应该更正循环中的 my_dir,因为它正在寻找一个名为“my_dir”的目录,替换为实际目录。此外,您应该只使用在循环中创建的文件路径引用,无需使用实际的字符串。

# import packages needed
import glob
import tabula

# transform the pdfs into excel files
for filepath in glob.iglob('C:/Users/myfolderwithpdfs/*.pdf'):
tabula.convert_into(filepath, output_format="xlsx")

关于python - Forloop用于将目录中的所有pdf转换为excel文件不起作用 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59288366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com