Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report