S
S
StenMarsh13372020-11-17 12:20:27
Python
StenMarsh1337, 2020-11-17 12:20:27

From file to Google Sheets using Python?

I need text from a word document to be inserted into Google Sheets line by line, starting at A3 and down the column.

import gspread
import json
import pyperclip
import config
from oauth2client.client import SignedJwtAssertionCredentials
varss = pyperclip.paste()
with open(config.text, 'a', encoding='utf-8') as f: f.write(str(varss))
scope = ['https://spreadsheets.google.com/feeds']
json_key = json.load(open('creds.json'))
creds = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
gc = gspread.service_account(filename='creds.json')
sh = gc.open("ADSS")
w = open("texts.txt")
for line in w:
    worksheet = sh.worksheet("1ss")
    worksheet.update_acell("A3", str(line))

1. Text document from which we take the text
5fb3959e179da220513686.png
2. How to add to Google Sheets:
5fb395d4f05fa608143423.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-11-17
@StenMarsh1337

This code does what you need.
You need to share the table for the service account, which is registered in the credentials.
Text file, read line by line and immediately write to a table

import gspread

sheet_key = 'id таблицы'
work_sheet_name ='название листа'

gc = gspread.service_account(filename='credentials.json')
sh = gc.open_by_key(sheet_key)
worksheet = sh.worksheet(work_sheet_name)

f = open('texts.txt', 'r', encoding='utf-8')

i = 3
for line in f:
    range = 'A'+str( i)
    worksheet.update(range, line)
    i = i+1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question