
18PDF 转 Word——利用 OCR 技术批量提取不可编辑的文档内容第三阶段PDF 与图片处理16-22场景引入收到一份扫描版 PDF 合同里面的文字无法复制、无法搜索、无法编辑。需要将其转换为可编辑的 Word 文档。手动逐字输入太慢用免费转换工具又有页数限制。本节教你用 Python 批量将 PDF 转为 Word特别是扫描件图片格式也能通过 OCR 提取文字。技术原理PDF 转 Word 分两种情况PDF 类型特点处理方式文字型文字可复制选择直接提取文字扫描型实质是图片需要 OCR 识别文字OCR光学字符识别流程扫描 PDF → 每页转为图片 → Tesseract OCR 识别文字 → 写入 Word 文档环境准备方案 1文字型 PDF简单pipinstallpdfplumber python-docx方案 2扫描型 PDFOCRpipinstallpytesseract pdf2image python-docx Pillow还需要安装 Tesseract OCR 引擎下载https://github.com/UB-Mannheim/tesseract/wiki安装后设置环境变量或在代码中指定路径完整代码方案 1文字型 PDF 转 WordimportpdfplumberfromdocximportDocumentfrompathlibimportPathdefpdf_to_word_text(input_pdf,output_docx): 将文字型 PDF 转换为 Word 文档 参数: input_pdf: 源 PDF 文件 output_docx: 输出 Word 文件 docDocument()withpdfplumber.open(input_pdf)aspdf:total_pageslen(pdf.pages)print(f共{total_pages}页开始转换...)fori,pageinenumerate(pdf.pages):# 提取文字textpage.extract_text()iftext:doc.add_paragraph(text)# 添加分页符除最后一页ifitotal_pages-1:doc.add_page_break()print(f已转换第{i1}页)doc.save(output_docx)print(f\n转换完成:{output_docx})# 提取表格defpdf_tables_to_word(input_pdf,output_docx): 将 PDF 中的表格提取到 Word docDocument()withpdfplumber.open(input_pdf)aspdf:fori,pageinenumerate(pdf.pages):tablespage.extract_tables()fortable_idx,tableinenumerate(tables):iftable:doc.add_heading(f第{i1}页 - 表格{table_idx1},level2)# 创建 Word 表格word_tabledoc.add_table(rowslen(table),colslen(table[0]))forrow_idx,rowinenumerate(table):forcol_idx,cellinenumerate(row):word_table.cell(row_idx,col_idx).textstr(cellor)doc.save(output_docx)print(f表格提取完成:{output_docx})方案 2扫描型 PDFOCR转 Wordimportpytesseractfrompdf2imageimportconvert_from_pathfromdocximportDocumentimportos# Tesseract 路径根据实际安装位置修改TESSERACT_PATHrC:\Program Files\Tesseract-OCR\tesseract.exepytesseract.pytesseract.tesseract_cmdTESSERACT_PATHdefocr_pdf_to_word(input_pdf,output_docx,langchi_simeng): 使用 OCR 将扫描版 PDF 转为可编辑 Word 参数: input_pdf: 扫描版 PDF output_docx: 输出 Word lang: OCR 语言chi_sim简体中文eng英文可组合 docDocument()# PDF 每页转为图片print(正在将 PDF 转为图片...)imagesconvert_from_path(input_pdf,dpi300)print(f共{len(images)}页)fori,imageinenumerate(images):print(fOCR 识别第{i1}页...)# OCR 识别textpytesseract.image_to_string(image,langlang)# 写入 Worddoc.add_paragraph(text)ifilen(images)-1:doc.add_page_break()doc.save(output_docx)print(f\nOCR 转换完成:{output_docx})方案 3批量转换文件夹中的所有 PDFdefbatch_pdf_to_word(folder_path,output_dirWord文档,use_ocrFalse): 批量转换文件夹中的所有 PDF folderPath(folder_path)outputPath(output_dir)output.mkdir(parentsTrue,exist_okTrue)forpdf_fileinfolder.glob(*.pdf):word_fileoutput/f{pdf_file.stem}.docxifuse_ocr:ocr_pdf_to_word(str(pdf_file),str(word_file))else:pdf_to_word_text(str(pdf_file),str(word_file))print(f\n批量转换完成! 输出目录:{output_dir}/)常见问题Q1OCR 识别率低提高 DPIconvert_from_path(input_pdf, dpi600)— DPI 越高识别越准确图像预处理转换为灰度、二值化后再识别语言包确保安装了正确的语言包Q2中文识别出来是乱码确保 Tesseract 安装了中文语言包下载chi_sim.traineddata放到Tesseract-OCR\tessdata\目录使用langchi_sim参数Q3转换后格式丢失OCR 只能提取文字无法保留原始格式字体、图片、表格等。如需保留格式建议使用专业工具如 Adobe Acrobat、WPS 会员版。总结PDF 类型方案准确率速度文字型pdfplumber100%快扫描型Tesseract OCR85-95%中等含表格pdfplumber.extract_tables()高快本节掌握了 PDF 转 Word 的两种方法文字型直接提取 扫描型 OCR 识别。下一节预告19图片水印添加——一键为数千张员工工牌或产品图加盖 Logo