{"id":41594,"date":"2021-08-12T09:33:08","date_gmt":"2021-08-12T07:33:08","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=41594"},"modified":"2021-08-12T09:33:08","modified_gmt":"2021-08-12T07:33:08","slug":"python-script","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/script-de-python\/","title":{"rendered":"Script to convert PDF to TXT with Python"},"content":{"rendered":"<p>Today we teach you how to create a simple<strong> python script<\/strong> which will convert a PDF file to txt.\u00a0<\/p>\n\n\n\n<p>If you want to become an expert with the necessary skills to work with Python, with the <a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/eiposgrados.com\/master-en-python\/\">Master in Advanced Programming in Python for Hacking, BigData and Machine Learning<\/a> You will be trained in just 12 months. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to follow<\/h2>\n\n\n\n<p>The first thing is <strong>create a PDF file<\/strong> or look for one we have. We can do this through Word by saving any document in PDF File &gt; Save as\u2026<\/p>\n\n\n\n<p>Need <strong>install PyPDF2<\/strong>, a Python PDF library that can split, merge, crop, and transform PDF files. According to the PyPDF2 website it can also be used to add data, viewing options and passwords to PDF files.<\/p>\n\n\n\n<p>To install the PyPDF2 package, we will only have to write in the Windows command prompt or in the terminal of our favorite IDLE <strong>pip install PyPDF2<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/script-en-python-1024x275.png\" alt=\"\" class=\"wp-image-41596\" width=\"717\" height=\"192\" title=\"\"><\/figure><\/div>\n\n\n\n<p>Later we have to create a new Python file in the same location where we will have saved the pdf file and write our code.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/script-con-python-1024x578.png\" alt=\"python script that will convert a file to PDF to txt\" class=\"wp-image-41597\" width=\"645\" height=\"364\" title=\"\"><\/figure><\/div>\n\n\n\n<p>Although we have detailed the code, we give you a quick explanation of it:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First we create a <strong>Python file object<\/strong> and we open the PDF file in read binary (rb) mode.<\/li><li>We create the <strong>PdfFileReader object<\/strong> which will read the open file.<\/li><li><strong>We will use a variable<\/strong> to store the number of pages in the file.<\/li><li>Finally <strong>we will indicate the path of the txt file<\/strong> where lines from the PDF file will be written.<\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p>We teach you how to create a simple Python script that will convert a PDF file to TXT, with all the steps you need to follow. Do not miss it!<\/p>","protected":false},"author":1,"featured_media":41602,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-41594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/41594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=41594"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/41594\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/41602"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=41594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=41594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=41594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}