#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from FileProcess import *
import poocr
import pdfplumber
from PIL import Image

# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    pdf: str
    fileExt = ".pdf"
    fileNewExt = ".jpg"

    try:
        invoicesPath = getInvoicesDir()
        invoicesFile = getAllFiles(invoicesPath,fileExt)
        for file in invoicesFile:
            jpgFile = ChangeFileExt(file, fileNewExt)
            with pdfplumber.open(file) as pdf:
                page1 = pdf.pages[0]
                im = page1.to_image(resolution=100)  #
                im.save(jpgFile)

        output = r'allinvoices.xlsx'
        config = r'poocr-config.toml'
        poocr.ocr2excel.VatInvoiceOCR2Excel(input_path=invoicesPath, output_excel=output, configPath=config)
    except Exception as e:
        print(e)
    finally:
        print("translate sucessed!")
        pass
