在企业办公室场景中,每天都会产生大量包含重要信息的PDF文档,如合同文件、财务报表、项目计划书等。这些文档在进行存档时,通常需要有清晰、规范且易于识别的文件名,以便后续快速检索和管理。使用该功能,可以批量对这些PDF文件中的文字进行OCR识别,提取如合同编号、项目名称、报表日期等关键信息作为新文件名,大大提高了档案整理的效率和准确性,方便员工在需要时能够迅速定位和查阅所需的文档。
要实现批量OCR识别PDF中的文字,并根据文字对PDF进行批量重命名,可以使用QT作为GUI框架,结合腾讯云的OCR API来实现。以下是详细的开发步骤:
PyPDF2
或pdfminer
等Python库来处理PDF文件。PyPDF2
或pdfminer
提取PDF中的每一页,并将其保存为图片。os
模块重命名PDF文件。以下是一个简化的示例代码,展示如何在QT中调用腾讯云OCR API并重命名PDF文件:
python复制import sys
import os
import json
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog, QLabel, QVBoxLayout, QWidget
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.ocr.v20181119 import ocr_client, models
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PDF OCR Rename')
self.setGeometry(100, 100, 400, 200)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout()
self.select_button = QPushButton('选择PDF文件夹', self)
self.select_button.clicked.connect(self.select_folder)
self.layout.addWidget(self.select_button)
self.rename_button = QPushButton('开始OCR并重命名', self)
self.rename_button.clicked.connect(self.start_rename)
self.layout.addWidget(self.rename_button)
self.status_label = QLabel('', self)
self.layout.addWidget(self.status_label)
self.central_widget.setLayout(self.layout)
self.folder_path = ''
self.secret_id = 'your_secret_id'
self.secret_key = 'your_secret_key'
def select_folder(self):
self.folder_path = QFileDialog.getExistingDirectory(self, '选择PDF文件夹')
if self.folder_path:
self.status_label.setText(f'已选择文件夹: {self.folder_path}')
def start_rename(self):
if not self.folder_path:
self.status_label.setText('请先选择文件夹')
return
for filename in os.listdir(self.folder_path):
if filename.endswith('.pdf'):
pdf_path = os.path.join(self.folder_path, filename)
new_name = self.process_pdf(pdf_path)
if new_name:
new_path = os.path.join(self.folder_path, new_name)
os.rename(pdf_path, new_path)
self.status_label.setText(f'重命名成功: {filename} -> {new_name}')
def process_pdf(self, pdf_path):
# 将PDF每一页转换为图片(此处省略具体实现)
# 假设每一页保存为image_0.jpg, image_1.jpg, ...
for page_num in range(0, 10): # 假设PDF有10页
image_path = f'image_{page_num}.jpg'
# 调用OCR API
text_detections = self.ocr_general_basic(image_path, self.secret_id, self.secret_key)
# 提取关键信息(如文件名)
for detection in text_detections:
if '识别结果' in detection:
extracted_text = detection['DetectedText']
# 假设提取的文本可以作为新文件名
new_name = f'{extracted_text}.pdf'
return new_name
return None
def ocr_general_basic(self, image_path, secret_id, secret_key):
cred = credential.Credential(secret_id, secret_key)
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-guangzhou", clientProfile)
req = models.GeneralBasicOCRRequest()
with open(image_path, 'rb') as f:
img_data = f.read()
params = {
"ImageBase64": img_data.encode('base64')
}
req.from_json_string(json.dumps(params))
resp = client.GeneralBasicOCR(req)
return json.loads(resp.to_json_string())['TextDetections']
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
pdf2image
库或其他工具。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有