


Python realizes PD text recognition, extraction and writing into CSV file script sharing
1. Foreword
2. Requirements description
3. Start using your brain
3.1 Install relevant third-party packages
3.2 Import the required third-party libraries
3.3 Read the pdf file and identify the content
3.4 Process the identified data and write it to the csv file
Summary
1. Foreword
Scanned documents have always been popular among the public. Any paper documents can be archived after being scanned, and can be opened on your mobile phone when you want to use them, saving worry and effort. However, the advantages of the scanned document also lead to a disadvantage. Because it is scanned through an electronic device, what comes out is an image. If you want to process the content on the file, direct operation is not possible.
What if you want to quote the content? Don't worry, Python will help you solve the problem.
2. Requirement Description
There is a pdf scan. We want to extract the text and write it into a csv document in three columns. The content and effect are as follows:
pdfexample
csvexample
3. Start using your hands and brain
The pdf scan is a document scan It is converted into a computer picture format, and extracting the text is equivalent to identifying the text in the picture. Therefore, our job is to convert the PDF into a picture, and then use the OCR tool to extract the text in the picture.
3.1 Install relevant third-party packages
pip3 install pdf2image pytesseract
3.2 Import the required third-party libraries
import os #处理文件 from pdf2image import convert_from_path# pdf转图片 import pytesseract# 识别图片文字 import csv# 处理csv文件
3.3 Read pdf files and identify the content
tess_ocr(pdf_path, lang, first_page, last_page)
Split the pdf file into pictures, and extract the text and write it into the text file
- pdf_path: The storage path of the pdf file
- image: List of PIL images representing each page of the PDF document
- first_page: allows setting the first page processed by pdftoppm;
- last_page: allows setting the last page processed by pdftoppm
- fmt: Allows specifying the output format. Currently supported formats are jpg, png and ppm;
- output_folder: image saving path
def tess_ocr(pdf_path, lang,first_page,last_page): # 创建一个和pdf同名的文件夹 images = convert_from_path(pdf_path, fmt='png',first_page=first_page,last_page=last_page,output_folder=imagefolder,userpw='site')# 转成图片 text = '' for img in images: text += pytesseract.image_to_string(img, lang=lang) # 识别图片文字 with open(r'exampledata.txt' 'a', encoding='utf-8') as f: #写入txt文件 f.write(text)
Running results
Generate a folder with the same name to store the split Picture, then extract the picture text and write it into data.txt
image-20211215212147760
Running problem
" " Problem throws 1:
pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH? ”
Solution: Download poppler.
>1 Method 1: Set the environment variable poppler/bin;
>2 Method 2:
The parameter specifies the absolute path:
images = convert_from_path( pdf_path=pdf_file_path, poppler_path=r'the address of the bin file in poppler')
" " Problem throws 2:
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH . See README file for more information. ”
Solution: additionally download and install tesseract-ocr and configure environment variables.
3.4 Process the identified data and write it into a csv file
modification(infile, outfile)
Clean the generated text document
- infile: The address of the file that needs to be processed
- outfile: The address of the new file generated after processing
def modification(infile, outfile): infp = open(infile, "r",encoding='utf-8') outfp = open(outfile, "w",encoding='utf-8') lines = infp.readlines() #返回列表,包含所有的行。 #依次读取每行 for li in lines: if li.split(): #str.split(str="", num=string.count(str)),过滤文件中的空行 # 根据识别情况对数据进行清洗 li = li.replace('[', ' ').replace(']', '') outfp.writelines(li) infp.close() outfp.close()
Running result
Generate a New txt file, the new file deletes the blank lines in data.txt, and replaces the incorrectly identified content in the original file with the correct one.
writercsv(intxt,outcsv)
Write the text file into the csv table separated by spaces
- intxt: text file address
- outcsv: Newly generated csv file
def writercsv(intxt,outcsv): # 使用newlines=''可保证存储的数据不空行。 csvFile = open(outcsv, 'a',newline='', encoding='utf-8') writer = csv.writer(csvFile) csvRow = [] f = open(intxt,'r',encoding='utf-8') for line in f: csvRow = line.split() #以空格为分隔符 if len(csvRow)>1 and len(csvRow)<=3:#约束条件,视情况而定 writer.writerow(csvRow) f.close() csvFile.close()
Running result
Generates a three-column csv file, the first column is the English name, and the second column is the Chinese name , the third column is the country
image-20211215204846623
image-20211215204941725
Summary
Through this study, it is possible to extract text from scanned documents and write the content into different formats as required. Documentation requirements.
At first I thought that the library for extracting pdf would also be suitable for scanned documents, so I tried the Pdfplumber library and the PyPDF2 library.
Practice found that Pdfplumber can only recognize watermarks in scanned PDFs, and is not applicable to scanned PDFs. The PyPDF2 library runs and reports an error: NotImplementedError: only algorithm code 1 and 2 are supported.
The reason is that this encrypted PDF may come from a higher version of acrobot, so the corresponding encryption algorithm code is '4'. However, the existing pypdf2 module only supports the encryption algorithm code '4'. 1' or '2' pdf encrypted file.
The above is the detailed content of Python realizes PD text recognition, extraction and writing into CSV file script sharing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
