Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the pdf effect of converting text pdf into Picture by Python

Shulou Source: shulou.com Published: 2022-06-01 01:31:51 09月21日 Update

This article mainly explains "Python how to achieve text pdf conversion picture pdf effect", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python how to achieve text pdf conversion picture pdf effect" bar!

Code presentation

Install dependencies first

Pip install PyMuPDF

Enter a list of files

Import osimport fitzdef single_wordpdf_to_imgpdf (pdf_path: str): pdf = fitz.open (pdf_path) # Open the pdf directory pdf_img = fitz.open () # Open the empty file, which is used to store the picture pdf for page_inf in pdf: definition = 3 # definition. I feel that the output pdf is not clear enough and can be zoomed up. The file size also becomes larger matrix= fitz.Matrix (definition, definition) img = page_inf.get_pixmap (matrix=matrix). Tobytes () img = fitz.open ("png", img) pdf_bytes = img.convert_to_pdf () pdf_img.insert_pdf (fitz.open ("pdf") Pdf_bytes) pdf_img.save ("Picture pdf_" + os.path.basename (pdf_path)) def group_wordpdf_to_imgpdf (path_array: list [str]): for pdf_path in path_array: print (pdf_path, "converting...") Single_wordpdf_to_imgpdf (pdf_path) print ("done") if _ _ name__ = ='_ main__': path = ["xxx.pdf"] group_wordpdf_to_imgpdf (path)

Enter folder

Import osimport fitzimport timedef single_wordpdf_to_imgpdf (pdf_path: str): pdf = fitz.open (pdf_path) # Open the pdf directory pdf_img = fitz.open () # Open the empty file, which is used to store the picture pdf for page_inf in pdf: definition = 3 # definition. I feel that the output pdf is not clear enough and can be zoomed up. The file size also becomes larger matrix= fitz.Matrix (definition, definition) img = page_inf.get_pixmap (matrix=matrix). Tobytes () img = fitz.open ("png", img) pdf_bytes = img.convert_to_pdf () pdf_img.insert_pdf (fitz.open ("pdf") Pdf_bytes) if not os.path.exists ("output"): os.makedirs ("output") # the processed pdf is stored in the output directory # pdf_img.save ("output/ image pdf_" + os.path.basename (pdf_path)) def group_wordpdf_to_imgpdf (path_array: list [str]): for pdf_path in path_array: print (pdf_path, "converting...") Single_wordpdf_to_imgpdf (pdf_path) print ("done") def folder_pdf_files (folder: str)-> list [str]: # how many pdf files are there in a folder file_list = [] for a, b C in os.walk (folder): if b = = []: for filename in c: if filename [- 3:] .lower () = 'pdf': file_path = os.path.join (a, filename) file_list.append (file_path) print (folder, ": yes", len (file_list) "pdf files") return file_listif _ _ name__ ='_ _ main__': time_start = time.time () path_list = folder_pdf_files ("directory") group_wordpdf_to_imgpdf (path_list) time_end = time.time () print ("program run time:", round (time_end-time_start, 2), "seconds") I believe that everyone on the "Python how to achieve text pdf conversion picture pdf effect" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Files pictures directories effects text not enough content size feel folder sharpness learning input output practical deeper code interest practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information OPPO Reno Linux macOS