Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Text Files as Google Keep Notes
#Import a directory of text files as google keep notes.
#Text Filename is used for the title of the note.
import gkeepapi, os
username = 'username@gmail.com'
password = 'your app password'
keep = gkeepapi.Keep()
success = keep.login(username,password)
dir_path = os.path.dirname(os.path.realpath(__file__))
for fn in os.listdir(dir_path):
if os.path.isfile(fn) and fn.endswith('.txt'):
with open(fn, 'r') as mf:
data=mf.read()
keep.createNote(fn.replace('.txt',''), data)
keep.sync();
@mguellsegarra

This comment has been minimized.

Copy link

mguellsegarra commented Mar 30, 2018

Thanks for this, you saved me a lot of time :)

@amirhmoradi

This comment has been minimized.

Copy link

amirhmoradi commented Aug 28, 2018

Great! Thanks for this, saved me lots of time!

@laserjeyes

This comment has been minimized.

Copy link

laserjeyes commented Sep 8, 2018

Thanks for the script! On my Jupyter notebook on Windows I encountered the following problems:

  • For some mysterious reason, dir_path stayed at "E:\\Desktop", the actual folder on the desktop which I had indicated was ignored. Therefore I moved the txt-files to the desktop.
  • os.path.isfile(fn) was never true so I deleted this part of the if-clause.
  • open(fn, 'r') caused a file not found error and I had to replace it with open(str(dir_path)+'\\'+fn, 'r')

Then, it did its job perfectly!

@rostok

This comment has been minimized.

Copy link

rostok commented Sep 9, 2019

Great stuff! I had problem importing filenames with special UTF characters yet script did the job.
For other users - you can add dedicated app/script password on Google account on https://myaccount.google.com/u/1/apppasswords

@onety-two

This comment has been minimized.

Copy link

onety-two commented Nov 8, 2019

It did not upload all my files. What should I do? I have over 1000 notes I want moved over. It stopped about halfway. Thank you in advance.

@rostok

This comment has been minimized.

Copy link

rostok commented Nov 8, 2019

@onety-two check if your filenames are pure ASCII. I had trouble with polish letters and script had no error message for this.

@sliceofbytes

This comment has been minimized.

Copy link
Owner Author

sliceofbytes commented Nov 8, 2019

@onety-two I would just check which file it stopped at (add a print), likely it's an ascii / utf8 char issue. This was just a quick script for a friend.

@joshhboss

This comment has been minimized.

Copy link

joshhboss commented Nov 18, 2019

I am a complete newb.. i am just trying to take all of my txt notes on my windows machine and add them to google keep.. just moved over from mac to windows..

how and where do i add that code

@Sebastian7700

This comment has been minimized.

Copy link

Sebastian7700 commented Feb 25, 2020

For me, it raises a login exception, if someone could help I'd be really thankful.
So here is what I did:

  1. In the folder with all my .txt files, I created a file called "importkeep.py" in which I copied all of the code provided and changed username and password to mine.
  2. I pip installed "gkeepapi".
  3. I opened cmd and typed "C:\Users\Sebastian\AppData\Local\Programs\Python\Python38\python.exe C:\Users\Sebastian\Desktop\txtfiles\importkeep.py"
  4. This is the error code I get in cmd:
    "Traceback (most recent call last):
    File "C:\Users\Sebastian\Desktop\txtfiles\importkeep.py", line 10, in
    success = keep.login(username,password)
    File "C:\Users\Sebastian\AppData\Local\Programs\Python\Python38\lib\site-packages\gkeepapi_init_.py", line 693, in login
    ret = auth.login(username, password, get_mac())
    File "C:\Users\Sebastian\AppData\Local\Programs\Python\Python38\lib\site-packages\gkeepapi_init_.py", line 59, in login
    raise exception.LoginException(
    gkeepapi.exception.LoginException: ('NeedsBrowser', 'To access your account, you must sign in on the web. Touch Next to start browser sign-in.')"

SOLUTION:

  1. Enable 2-step authentication in your Google settings.
  2. Create an app password here: https://myaccount.google.com/apppasswords
  3. Type for the app "gkeepapi" and generate a password.
  4. Go to your code and put the generated password there instead of your Google password.

Thanks for the code, it helpt me import over 400 txt files.

@WiliTest

This comment has been minimized.

Copy link

WiliTest commented Mar 21, 2020

It did not work for me: gkeepapi.exception.LoginException: ('BadAuthentication', None)
I tried with the Gmail login and password, then with an "app password" (following @Sebastian7700 instruction)

@alils

This comment has been minimized.

Copy link

alils commented Mar 24, 2020

@WiliTest same problem. does anyone able to authenticate with app password?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.