{"id":3153,"date":"2023-06-09T15:36:22","date_gmt":"2023-06-09T23:36:22","guid":{"rendered":"https:\/\/crm270s.wordpress.com\/?p=3153"},"modified":"2023-06-09T15:36:22","modified_gmt":"2023-06-09T23:36:22","slug":"phone-extract-python-code","status":"publish","type":"post","link":"https:\/\/crm270s.mycpamytax.com\/?p=3153","title":{"rendered":"phone extract PYTHON code"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import re\nimport PyPDF2\nimport importlib\nimport subprocess\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# Specify the path to your PDF file\npdf_file_path = 'sample.pdf'\n\n# Extract phone numbers from the PDF file\nphone_numbers = extract_phone_numbers_from_pdf(pdf_file_path)\n\n# Print the extracted phone numbers\nfor number in phone_numbers:\n    print(number)\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-3153","post","type-post","status-publish","format-standard","hentry","category-codes"],"_links":{"self":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/3153","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=3153"}],"version-history":[{"count":0,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/3153\/revisions"}],"wp:attachment":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}