呼叫百度ocr實現PDF轉文字

2022-09-09 15:06:37 字數 4895 閱讀 3746

#讀取函式

defocr(path):

with open(path, 'rb

') as f:

return

f.read()

deflink_ocr(client,image):

for i in range(5):

try:

dict1 =client.basicgeneral(image)

except

:

continue

else

:

return

dict1

return

''def

ocr_way(path):

global

global

api_key

global

secret_key

print("

正在將上傳到識別介面,請稍後....")

#讀取image =ocr(path)

#程序ocr識別

dict1=link_ocr(client,image)

#dict1 = client.general(image)

if dict1==''

:

return''#

print(dict1)

while

"words_result

"not

indict1.keys():

print

(dict1)

return""#

print("請求超時,正在重新上傳該頁面")

#dict1 = client.general(image)

result_list =

print("

該頁面文字識別完成")

for i in dict1["

words_result"]:

'words'])

return"\n

".join(result_list)

defpymupdf_fitz(pdfpath, imagepath):

''':param pdfpath:

:param imagepath:

迴圈pdf的每一頁,生成jpg寫入到temp_pic資料夾裡

:return:

'''global

times

print(f"

開始讀取")

pdfdoc =fitz.open(pdfpath)

text_list =

for pg in

range(pdfdoc.pagecount):

print(f"

正在讀取的第頁")

page =pdfdoc[pg]

rotate =int(0)

#每個尺寸的縮放係數為1.3,這將為我們生成解析度提高2.6的影象。

#將寫入指定的資料夾內

"可能出錯\n")

#讀取配置檔案

log =configparser()

log.read(

"files.log")

pdf_path = log.get('

files_path

', '

pdf_path')

pic_path = log.get('

files_path

', '

pic_path')

excel_path = log.get('

files_path

', '

excel_path')

#為配置檔案中的路徑建立資料夾

ifnot

os.path.exists(pdf_path):

os.makedirs(pdf_path)

ifnot

os.path.exists(pic_path):

os.makedirs(pic_path)

ifnot

os.path.exists(excel_path):

os.makedirs(excel_path)

#生成即將建立的**的名稱

time_list =list(time.localtime(time.time()))

time_str_list = [str(i) for i in

time_list]

time_line = "

_".join(time_str_list[:6])

table_name = f"/"

if file_type=='

excel':

#新建xlsx

workbook = xlsxwriter.workbook(table_name+"

.xlsx")

worksheet = workbook.add_worksheet('

sheet1')

heading = ['

pdf名稱

', '

識別內容']

row =0

col =0

worksheet.write_row(row, col, heading)

#獲取pdf內容

pdf_list =os.listdir(pdf_path)

for pdf_file in

pdf_list:

result_text = pymupdf_fitz(f"/"

, pic_path)

row += 1worksheet.write_row(row, col, [pdf_file, result_text])

workbook.close()

elif file_type == '

csv'

:

#新建csv

heading = ['

pdf名稱

', '

識別內容']

with open(table_name+"

.csv

", '

a', newline=""

)as f:

f_csv =csv.writer(f)

f_csv.writerow(heading)

pdf_list =os.listdir(pdf_path)

for pdf_file in

pdf_list:

result_text = pymupdf_fitz(f"/"

, pic_path)

with open(table_name+"

.csv

", '

a', newline=""

)as f:

f_csv =csv.writer(f)

f_csv.writerow([pdf_file,result_text])

else

:

print("

配置檔案型別選擇錯誤")

f_error.close()

[files_path]

pdf_path=pdf

pic_path=pic

excel_path=excel

[png_size]

times=2.5

[type]

type=csv

files.log 檔案

Python實現PDF轉文字

安裝依賴 pip install pdfminer3k coding utf 8 import sys import importlib importlib.reload sys from pdfminer.pdfparser import pdfparser,pdfdocument from pd...

python 使用百度AI實現OCR文字識別

執行pip install baidu aip from aip import aipocr 更換為自己的註冊資訊 api key secret key fp open img.png rb read 開啟並讀取檔案內容 res client.basicgeneral fp 普通 print res...

百度AI的OCR通用文字識別呼叫改成鏈結訪問

問題是我肯定是有許可權的,只好換成鏈結直接訪問的形式 posturl url 你的apikey client secret 你的secretkey urlcontent file get contents url urlcontent json decode urlcontent,true acce...