{"id":3156,"date":"2023-06-09T15:47:01","date_gmt":"2023-06-09T23:47:01","guid":{"rendered":"https:\/\/crm270s.wordpress.com\/?p=3156"},"modified":"2023-06-09T15:47:01","modified_gmt":"2023-06-09T23:47:01","slug":"phone-extract-python-code-final","status":"publish","type":"post","link":"https:\/\/crm270s.mycpamytax.com\/?p=3156","title":{"rendered":"phone extract PYTHON code &#8211; FINAL"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import re\nimport PyPDF2\nimport importlib\nimport subprocess\nimport csv\nimport os\n\ndef install_missing_package(package):\n    try:\n        importlib.import_module(package)\n    except ImportError:\n        subprocess.check_call(&#091;'pip', 'install', package])\n\ndef extract_phone_numbers_from_pdf(file_path):\n    phone_numbers = &#091;]\n    \n    # Open the PDF file\n    with open(file_path, 'rb') as file:\n        # Create a PDF reader object\n        reader = PyPDF2.PdfReader(file)\n        \n        # Iterate over each page in the PDF\n        for page in reader.pages:\n            # Extract the text from the page\n            text = page.extract_text()\n            \n            # Use regular expressions to find phone numbers\n            # This pattern matches US phone numbers in various formats\n            pattern = r'(\\+\\d{1,2}\\s?)?(\\()?(\\d{3})(?(2)\\))&#091;-.\\s]?(\\d{3})&#091;-.\\s]?(\\d{4})'\n            matches = re.findall(pattern, text)\n            \n            # Format the phone numbers as (###) ###-####\n            formatted_numbers = &#091;'({}) {}-{}'.format(*match&#091;2:]) for match in matches]\n            \n            # Add the matched phone numbers to the list\n            phone_numbers.extend(formatted_numbers)\n    \n    return phone_numbers\n\n# Check if PyPDF2 is installed, and install it if necessary\ninstall_missing_package('PyPDF2')\n\n# Read the text file with a list of PDF file names\npdf_list_file = 'pdf_files.txt'\nwith open(pdf_list_file, 'r') as file:\n    pdf_files = file.read().splitlines()\n\n# Extract phone numbers from each PDF and store them in a list\nphone_number_list = &#091;]\nfor pdf_file in pdf_files:\n    phone_numbers = extract_phone_numbers_from_pdf(pdf_file)\n    phone_number_list.extend(&#091;(pdf_file, number) for number in phone_numbers])\n\n# Write the extracted phone numbers to a CSV file\ncsv_file = 'phone_extracts.csv'\nwith open(csv_file, 'w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerow(&#091;'PDF Name', 'Phone Number'])\n    writer.writerows(phone_number_list)\n\n# Print the success message\nprint(f\"Phone numbers extracted from {len(pdf_files)} PDF files and saved to {csv_file}.\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[26],"tags":[],"class_list":["post-3156","post","type-post","status-publish","format-standard","hentry","category-codes"],"_links":{"self":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/3156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3156"}],"version-history":[{"count":0,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/3156\/revisions"}],"wp:attachment":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}