0

Currently I am going through the automate the boring stuff with python video course and am basically copying his shown code and trying to create the program that way, my code is currently the exact same as in the video.

1st I will state the function of the program intended than 2nd why I think it might be having the error stated above and would appreciate someone helping the noob out on solving this.

  1. The program is intended take a pdf document that we have copied to the clipboard, the document contains emails and phone numbers. We want it to extract the email and phone and than copy that info at the end to the clipboard.

  2. What is different between the instructor and I is I am using a different document to copy and extract phone numbers as the one he used no longer exists. The current output if I print the phone numbers copied is the letter u and than the intended phone number

Current program output

 import re, pyperclip

    # Create a regex for phone numbers 
    phoneRegex = re.compile(r''' 
    (((\d\d\d)|(\(\d\d\d\)))?   # area code <optional>
    (\s|-)      # first seperator 
    \d\d\d      # first 3 digitis
    -           # seperator 
    \d\d\d\d    # last 4 digits 
    (((ext(\.)?\s)|x)   # extension word-part<optional>
    (\d{2,5}))?     # extension number-part<optional> 2,5 is to signify that it can be 2-5 digits
    )
    ''', re.VERBOSE)

    # TODO:: Create a regex object for email addresses 
    emailRegex = re.compile(r'''
    # we will make it search for emails that contain any numbers, letters plus or period symbols 

    [a-zA-Z0-9_.+]+     # name part
    @                   # @ symbol
    [a-zA-Z0-9_.+]+     # domain part

    ''', re.VERBOSE)

    # Get the text off the clipboard 
    text = pyperclip.paste()

    # Extract the email/phone from this text
    extractedPhone = phoneRegex.findall(text)
    extractedEmail = emailRegex.findall(text)

    allPhoneNumbers = []
    for phoneNumber in extractedPhone:
        allPhoneNumbers.append(phoneNumber[0])

    print(allPhoneNumbers)


    # TODO: Copy the extracted email/phone to the cliboard 
    results = '\n'.join(allPhoneNumbers) + '\n' + '\n'.join(extractedEmail)
    pyperclip.copy(results)
Zoe
  • 27,060
  • 21
  • 118
  • 148

3 Answers3

1

You can try to make allPhoneNumbers list variable as string variable and instead of appending it just concatenate the string, because pyperclip.paste() or pyperclip.copy() function is expecting string as argument. Here you are trying to insert a list into the paste() function that is the reason even i was getting the same error then just convert the list to string then will work perfectly. And one more important thing findAll() function always return a tuple so convert it also in string before concatenating to the resultant string.

A. K. P
  • 11
  • 1
0

I just had the same issue and I see it is probably a regression in pyperclip.

Try installing an older version. This worked for me.

python2 -m pip install 'pyperclip<1.6.2'

or simply

pip install 'pyperclip<1.6.2'

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
MarkJ
  • 171
  • 2
  • 7
0

This issue has been fixed in Pyperclip 1.6.5. You just need to upgrade by running pip install -U pyperclip

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92