I am getting all the data present in google sheet using code below, i want to write all these data to the pdf file and download that.
import gspread
import sys
print(sys.path)
import os
#sys.path.append('/usr/lib/python3/dist-packages')
from oauth2client.service_account import ServiceAccountCredentials
scope = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
path = os.path.abspath('cred.json')
credentials=ServiceAccountCredentials.from_json_keyfile_name('cred.json',scope)
client=gspread.authorize(credentials)
sheet=client.open('xyz').sheet1
data=sheet.get_all_records()
print(data)
I believe your goal as follows.
xyz
as a PDF file using gspread with python and the service acccount.requests
library and the endpoint for exporting Spreadsheet to PDF are used.When the points are reflected to your script, it becomes as follows.
import gspread
import sys
print(sys.path)
import os
#sys.path.append('/usr/lib/python3/dist-packages')
from oauth2client.service_account import ServiceAccountCredentials
scope = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
path = os.path.abspath('cred.json')
credentials=ServiceAccountCredentials.from_json_keyfile_name('cred.json',scope)
client=gspread.authorize(credentials)
# I added below script
spreadsheet_name = 'xyz'
spreadsheet = client.open(spreadsheet_name)
url = 'https://docs.google.com/spreadsheets/export?format=pdf&id=' + spreadsheet.id
headers = {'Authorization': 'Bearer ' + creds.create_delegated("").get_access_token().access_token}
res = requests.get(url, headers=headers)
with open(spreadsheet_name + ".pdf", 'wb') as f:
f.write(res.content)
create_delegated("")
to create_delegated("email of the service account")
.I'm also trying to export gsheet as pdf, and when I attempt to run this code it says
name 'creds' is not defined. Did you mean 'credits'. Here's my code:
import gspread, os, sys
session = gspread.service_account (filename =name)
sheet = session.open ("vpo")
worksheet = sheet.sheet1
url = 'https://docs.google.com/spreadsheets/export?format=pdf&id=' + sheet.id
#Seems to be an issue with the following line.
headers = {"Authorization": "Bearer " + creds.create_delegated ("").get_access_token ().access_token}
res = requests.get (url, headers = headers)
with open ("vpo.pdf", "wb") as file:
file.write (res.content)
Would really appreciate if someone could tell me where I'm going wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.