好友
阅读权限10
听众
最后登录1970-1-1
|
有段时间没有用这个了,要不是楼上qq63还真没发现这货居然修改了服务器。。。。。。调到“ 文档解析”里去了。。。我说怎么一提交就报错呢。。。。
参考官方说明:https://cloud.tencent.com/document/api/866/104610
主要修改如下:
1.
[Python] 纯文本查看 复制代码 #httpProfile.endpoint = "ocr.tencentcloudapi.com" 之前
httpProfile.endpoint = "lke.tencentcloudapi.com" #修改后
2.
[Python] 纯文本查看 复制代码 #"FileType": "PDF", 取消pdf设置,直接删除改行代码
3.
[Python] 纯文本查看 复制代码 #common_client = CommonClient("ocr", "2018-11-19", cred, "ap-guangzhou", profile=clientProfile) 之前
common_client = CommonClient("lke", "2023-11-30", cred, "ap-guangzhou", profile=clientProfile) #现在
完整代码修改参考如下:
[Python] 纯文本查看 复制代码 import os
import tkinter as tk
from tkinter import filedialog, messagebox
import base64
import fitz # PyMuPDF
import json
from tencentcloud.common.common_client import CommonClient
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
def read_pdf_to_base64(paf_file):
with open(paf_file, 'rb') as pdf_file:
binary_data = pdf_file.read()
base64_encoded_data = base64.b64encode(binary_data)
return base64_encoded_data.decode('utf-8')
def decode_base64_to_markdown(base64_str):
decoded_bytes = base64.b64decode(base64_str)
return decoded_bytes.decode('utf-8')
def save_as_md_file(content, filename):
with open(filename, 'w', encoding='utf-8') as file:
file.write(content)
def ocr_markdown(base64_string, FileStartPageNumber, FileEndPageNumber, secret_id, secret_key):
cred = credential.Credential(secret_id, secret_key)
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "lke.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
params_set = {
#"FileType": "PDF",
"FileBase64": base64_string,
"FileStartPageNumber": FileStartPageNumber,
"FileEndPageNumber": FileEndPageNumber
}
params = json.dumps(params_set)
common_client = CommonClient("lke", "2023-11-30", cred, "ap-guangzhou", profile=clientProfile)
try:
response = common_client.call_json("ReconstructDocument", json.loads(params))
return response['Response']['MarkdownBase64']
except TencentCloudSDKException as err:
error_message = f"An error occurred: {err}\nPlease enter the correct API Secret ID and Secret Key."
messagebox.showerror("Error", error_message)
idkey_file = 'D:/idkey.txt'
if os.path.exists(idkey_file):
if messagebox.askyesno("Delete idkey.txt", "The idkey.txt file exists. Do you want to delete it?"):
os.remove(idkey_file)
messagebox.showinfo("Deleted", "idkey.txt file has been deleted. Please enter the correct API Secret ID and Secret Key.")
return None
def process_pdf(paf_file, secret_id, secret_key, output_dir):
base64_string = read_pdf_to_base64(paf_file)
doc = fitz.open(paf_file)
page_count = doc.page_count
markdown_output = ''
pdf_filename = os.path.splitext(os.path.basename(paf_file))[0]
output_filepath = os.path.join(output_dir, f"{pdf_filename}.md")
if page_count <= 10:
FileStartPageNumber = 1
FileEndPageNumber = page_count
output = ocr_markdown(base64_string, FileStartPageNumber, FileEndPageNumber, secret_id, secret_key)
if output:
markdown_output = decode_base64_to_markdown(output)
save_as_md_file(markdown_output, output_filepath)
else:
num = page_count // 10
num_last = page_count % 10
for i in range(num):
FileStartPageNumber = i * 10 + 1
FileEndPageNumber = i * 10 + 10
output = ocr_markdown(base64_string, FileStartPageNumber, FileEndPageNumber, secret_id, secret_key)
if output:
markdown_output += decode_base64_to_markdown(output) + '\n'
if num_last != 0:
FileStartPageNumber = num * 10 + 1
FileEndPageNumber = num * 10 + num_last
output = ocr_markdown(base64_string, FileStartPageNumber, FileEndPageNumber, secret_id, secret_key)
if output:
markdown_output += decode_base64_to_markdown(output) + '\n'
save_as_md_file(markdown_output, output_filepath)
def select_file():
file_path = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
file_entry.delete(0, tk.END)
file_entry.insert(0, file_path)
def select_output_dir():
output_dir = filedialog.askdirectory()
output_dir_entry.delete(0, tk.END)
output_dir_entry.insert(0, output_dir)
def start_conversion():
paf_file = file_entry.get()
output_dir = output_dir_entry.get()
# Check if idkey.txt exists and read the keys
idkey_file = 'D:/idkey.txt'
if os.path.exists(idkey_file):
with open(idkey_file, 'r') as file:
lines = file.readlines()
secret_id = lines[0].strip().split(': ')[1]
secret_key = lines[1].strip().split(': ')[1]
else:
secret_id = secret_id_entry.get()
secret_key = secret_key_entry.get()
if not secret_id or not secret_key:
messagebox.showerror("Error", "Please enter the API Secret ID and Secret Key.")
return
with open(idkey_file, 'w') as file:
file.write(f"API Secret ID: {secret_id}\n")
file.write(f"API Secret Key: {secret_key}\n")
if not secret_id or not secret_key:
messagebox.showerror("Error", "Please enter the API Secret ID and Secret Key.")
return
process_pdf(paf_file, secret_id, secret_key, output_dir)
# 创建主窗口
root = tk.Tk()
root.title("PDF to Markdown Converter")
# 创建并放置标签和文本框
tk.Label(root, text="Select PDF File:").grid(row=0, column=0, padx=10, pady=10)
file_entry = tk.Entry(root, width=50)
file_entry.grid(row=0, column=1, padx=10, pady=10)
tk.Button(root, text="Browse", command=select_file).grid(row=0, column=2, padx=10, pady=10)
tk.Label(root, text="Select Output Directory:").grid(row=1, column=0, padx=10, pady=10)
output_dir_entry = tk.Entry(root, width=50)
output_dir_entry.grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="Browse", command=select_output_dir).grid(row=1, column=2, padx=10, pady=10)
tk.Label(root, text="API Secret ID:").grid(row=2, column=0, padx=10, pady=10)
secret_id_entry = tk.Entry(root, width=50)
secret_id_entry.grid(row=2, column=1, padx=10, pady=10)
tk.Label(root, text="API Secret Key:").grid(row=3, column=0, padx=10, pady=10)
secret_key_entry = tk.Entry(root, width=50, show='*')
secret_key_entry.grid(row=3, column=1, padx=10, pady=10)
tk.Button(root, text="Convert", command=start_conversion).grid(row=4, column=1, pady=20)
# 运行主循环
root.mainloop()
|
|