python - 如何添加多个要转换为excel的pdf?
问题描述
我有将 pdf 转换为 excel 的程序,现在我想添加多个输入,即多个 pdf 将被一一转换。
我的代码如下:
from PIL import Image
import io
import pytesseract
from wand.image import Image as wi
import os
import cv2
import pandas as pd
import re
import numpy as np
import os
pdf = wi(filename= "pdfs/jaalna.pdf", resolution =300)
pdfImage = pdf.convert("jpg")
imageBlobs = []
for img in pdfImage.sequence:
imgPage = wi(image = img)
#img.filter(ImageFilter.EDGE_ENHANCE_MORE )
imageBlobs.append(imgPage.make_blob('jpg'))
recognized_text = []
for imgBlob in imageBlobs:
im = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(im, lang = 'eng1+mar1')
recognized_text.append(text)
newfile = open('aama.txt','w')
newfile.write(",".join(recognized_text))
#add a folder as input.
解决方案
你可以使用循环
for name in ["pdfs/jaalna.pdf", "other/file.pdf"]:
pdf = wi(filename=name, resolution=300)
# rest of code
或者您可以使用sys.argv
来获取名称
script.py pdfs/jaalna.pdf other/file.pdf other/third.pdf
和代码
import sys
for name in sys.argv[1:]:
pdf = wi(filename=name, resolution=300)
# rest of code
推荐阅读
- r - 在 dplyr 和 data.table 包中使用长度函数进行聚合
- javascript - 在与 API 调用关联的函数中:未捕获(承诺中)类型错误:无法读取未定义的属性“包含”
- node.js - 套接字 io:socket.use() 不验证传入的套接字连接
- python - 打印对象名称
- r - R中的高频包问题
- java - 在Java中多次连接相同的AudioInputStream
- python - 创建一个“.plot”函数以显示在 Cartopy 地图上
- php - 如何在 Laravel 中选中的复选框上插入输入文本值
- javascript - 无法向 Eureka 服务器注册 React 应用程序
- python - 将图片文件从多个子文件夹移动到父文件夹Python