1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| from paddleocr import PaddleOCR import os from docx import Document from docx.shared import Pt from PIL import Image import time
def ocr_images_to_doc(image_folder, output_doc_path): """ 识别指定文件夹中的所有图片文字并保存到Word文档 参数: image_folder: 包含手机截图的文件夹路径 output_doc_path: 输出的Word文档路径 (.docx) """ ocr = PaddleOCR( use_angle_cls=True, lang='ch', use_gpu=False, show_log=False ) doc = Document() doc.add_heading('手机截图文字识别结果', level=1) valid_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff'] image_files = [f for f in os.listdir(image_folder) if os.path.splitext(f)[1].lower() in valid_extensions] if not image_files: print(f"在文件夹 {image_folder} 中未找到图片文件") return print(f"发现 {len(image_files)} 张图片,开始识别...") for i, img_file in enumerate(sorted(image_files)): img_path = os.path.join(image_folder, img_file) try: with Image.open(img_path) as img: width, height = img.size orientation = "横版" if width > height else "竖版" doc.add_heading(f"图片 {i+1}: {img_file} ({orientation}, {width}x{height})", level=2) start_time = time.time() result = ocr.ocr(img_path, cls=True) elapsed = time.time() - start_time text_lines = [] if result is not None: for line in result: if line and line[0]: text = line[1][0] confidence = line[1][1] text_lines.append(f"{text} (置信度: {confidence:.2f})") if text_lines: paragraph = doc.add_paragraph() for line in text_lines: run = paragraph.add_run(line + '\n') run.font.size = Pt(12) doc.add_paragraph(f"识别耗时: {elapsed:.2f}秒") else: doc.add_paragraph("未识别到文字") print(f"已处理: {img_file} (耗时: {elapsed:.2f}s)") except Exception as e: doc.add_paragraph(f"处理图片 {img_file} 时出错: {str(e)}") print(f"处理 {img_file} 时出错: {e}") doc.save(output_doc_path) print(f"识别完成! 结果已保存至: {output_doc_path}")
if __name__ == "__main__": IMAGE_FOLDER = "screenshots" OUTPUT_DOC = "识别结果.docx" ocr_images_to_doc(IMAGE_FOLDER, OUTPUT_DOC)
|