[英]Python pdf2image convert to path not working using sftp
我想將掃描 pdf 轉換為 OCR pdf,如果文件在本地目錄中,我的代碼可以工作,但如果我使用 SFTP,就會出現問題。 當我使用 sftp 使用 convert_from_path 時,在使用 sftp 讀取文件路徑時error expected str, bytes or os.PathLike object, not SFTPFile
。
如何在 pdf2image convert_to_path 中讀取 SFTP 文件路徑?
這是我的代碼:
for path in sftp.listdir_attr(file_path):
file = file_path + path.filename
f_name = path.filename
with sftp.open(file) as my_file:
print(my_file)
pages = convert_from_path(my_file, 200)
sftpfile路徑output應該是這樣的,不是pathlib.path:
<paramiko.sftp_file.SFTPFile object at 0x7fa22ed55250>
<paramiko.sftp_file.SFTPFile object at 0x7fa22ed55160>
錯誤:TypeError: expected str, bytes or os.PathLike object, not SFTPFile
我不知道pdf2image。 但是convert_from_path
似乎不支持類似文件的對象。
所以要么你需要使用不同的pdf2image API(如果有的話)。
或者你將不得不求助於
將 SFTP 文件下載到臨時本地文件並讀取該文件。
或者下載SFTP文件到memory:
flo = BytesIO() sftp.getfo(file, flo) flo.seek(0) convert_from_path(flo.read(), 200)
請注意,以上是天真的內存效率低下的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.