简体   繁体   中英

Export data from the Google Sheet to PDF PYTHON

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.

  • You want to export Google Spreadsheet of xyz as a PDF file using gspread with python and the service acccount.

Modification points:

  • Unfortunately, it seems that in the current stage, the Spreadsheet cannot be directly export as a PDF file using gspread. So in this case, requests library and the endpoint for exporting Spreadsheet to PDF are used.

When the points are reflected to your script, it becomes as follows.

Modified script:

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)

Note:

  • In this modified script, it supposes that you hav ealready been able to get values from Google Spreadsheet using Sheets API. Please be careful this.
  • If an error related to Drive API, please enable Drive API at the API console.
  • If an error related to the service account, please modify 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM