Skip to content

Instantly share code, notes, and snippets.

@sliceofbytes
Created February 14, 2018 22:16
  • Star 26 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
Copy link

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

@amirhmoradi
Copy link

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

@johentsch
Copy link

johentsch 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
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
Copy link

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
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
Copy link
Author

@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
Copy link

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
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
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)
EDIT: it works with python 3.9 kiwiz/gkeepapi#81

@alils
Copy link

alils commented Mar 24, 2020

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

@mapomme1108
Copy link

Hello,

Thank you for your script!

It could be wery usefull because I have 400 text files to import but it does not work for me.
Same problem as WiliTest and alils

@mootensai
Copy link

mootensai commented Aug 21, 2020

for fn in os.listdir(dir_path):
	if os.path.isfile(os.path.join(dir_path, fn)) and fn.endswith('.txt'):
		with open(os.path.join(dir_path, fn), 'r', encoding='utf-8') as mf:
			data=mf.read()
			print(keep.createNote(fn.replace('.txt',''), data))
			print(keep.sync())
			time.sleep(5)

I modify it a little so it could support my 800++ notes because Google have limit for notes per minute

anyway thank you so much!

@IonceM
Copy link

IonceM commented Jul 31, 2021

So, here is the coding, if you want to add a label to your notes
In my case, i wanted to upload some text - songfiles (this is why you see .sng), and i wanted them splitted after the first "---" encounter.

for fn in os.listdir(dir_path):
if os.path.isfile(os.path.join(dir_path, fn)) and fn.endswith('.sng'):
with open(os.path.join(dir_path, fn), 'r', encoding='utf-16') as mf:
data = mf.read().split("---",1)[1]
pretty_fn = fn.replace('.sng','')
gnote = keep.createNote(pretty_fn, data)
label = keep.findLabel('YourLabelNameInGoogleKeep')
gnote.labels.add(label)
print(keep.sync())

@IonceM
Copy link

IonceM commented Jul 31, 2021

for fn in os.listdir(dir_path):
	if os.path.isfile(os.path.join(dir_path, fn)) and fn.endswith('.txt'):
		with open(os.path.join(dir_path, fn), 'r', encoding='utf-8') as mf:
			data=mf.read()
			print(keep.createNote(fn.replace('.txt',''), data))
			print(keep.sync())
			time.sleep(5)

I modify it a little so it could support my 800++ notes because Google have limit for notes per minute

anyway thank you so much!

Have got a question - I guess i have now a problem after trying to creates notes to fast. So now i get exceptions of rate_limit_Exceeded. Did you have the same problem? was in enought for you to wait for it to disapear or did you take action, to "release" your google account?

On Google Cloud Platform it states that only 30 Notes per minute are allowed for creation, by API. Since I exceeded this number, i am restricted since more then 3 hours now to use Google Keep (Even Reading/Syncing in the app is throwing me the same excepetion as in the image down here)

2021-07-31 19_54_39-Eingabeaufforderung

@mootensai
Copy link

You could try to increase time.sleep value

@IonceM
Copy link

IonceM commented Jul 31, 2021

You could try to increase time.sleep value

Well, the issue is already at the login. I added the time sleep value later on to my skript, but since I can't do anything....I guess i will wait till my account is released from the usage limit

@IonceM
Copy link

IonceM commented Jul 31, 2021

You could try to increase time.sleep value

Well, the issue is already at the login. I added the time sleep value later on to my skript, but since I can't do anything....I guess i will wait till my account is released from the usage limit

I found a blog where it states that the reset occurs after 24h. So waiting seems fine.
https://support.cloudm.io/hc/en-us/articles/360009862299-Reaching-or-exceeding-Google-API-quotas

@tiabenson
Copy link

tiabenson commented Aug 10, 2021

Thank you! Worked for me without having to change anything.

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

@joshhboss put your text and python files in the same directory, then run the .py file. You may have to pip install gkeepapi.

@WiliTest
Copy link

it works with python 3.9 kiwiz/gkeepapi#81

@Mactastic1-5
Copy link

Mactastic1-5 commented Nov 27, 2021

# This script needs to be in the same folder as the text files!

import time
import gkeepapi, os

username = '<username>' # Type your email address here
password = '<password>' # Use your app password here

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(os.path.join(dir_path, fn)) and fn.endswith('.txt'):
		with open(os.path.join(dir_path, fn), 'r', encoding='utf-8', errors='ignore') as mf:
			data=mf.read()
			print(keep.createNote(fn.replace('.txt',''), data))
			print(keep.sync())
			time.sleep(5)

I tried it on Windows Subsystem for Linux through Ubuntu, and it doesn't authenticate. I tried many of the suggestions, and the outcome was the same. It works on macOS. Maybe if I installed Python 3 natively with Chocolatey, it would work.

@tu-design
Copy link

tu-design commented Mar 9, 2022

I was facing two errors: 1) Notes not copying due to ascii 2) Notes not copying due to encoding, you can solve them as follows if you have any different encode of text files (here: UTF8) and also added a print option for logs to check which file gives the error.

import gkeepapi, os
username = 'username@gmail.com'
password = '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):
	print(fn)
	if os.path.isfile(fn) and fn.endswith('.txt'):
		with open(fn, encoding="utf8") as mf:
			data=mf.read()
			keep.createNote(fn.replace('.txt',''), data)
			keep.sync();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment