-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
66 lines (57 loc) · 1.9 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from datetime import datetime
import importlib
from oauth2client.service_account import ServiceAccountCredentials
import requests
from pdf2image import convert_from_path
import os
def get_class_from_string(class_path):
"""
Import a class from a string path.
Parameters
----------
class_path : str
The dot-separated path to the class.
Returns
-------
type
The class type.
"""
module_name, class_name = class_path.rsplit(".", 1)
module = importlib.import_module(module_name)
return getattr(module, class_name)
def download_page_in_pdf(credentials: ServiceAccountCredentials, spreadsheet_id: str, guid: str) -> str:
"""
Download a page from a Google Spreadsheet in PDF format.
Parameters
----------
credentials : ServiceAccountCredentials
The Google API credentials.
spreadsheet_id : str
The ID of the Google Spreadsheet.
guid : str
The GUID of the page to download.
"""
url = f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}/export?format=pdf&gid={guid}" \
"&portrait=false" \
"&fitw=true" \
"&scale=2" \
"&size=A4" \
"&top_margin=0.25&bottom_margin=0.25&left_margin=0.25&right_margin=0.25" \
"&gridlines=false" \
"&printtitle=false" \
"&sheetnames=false"
headers = {
"Authorization": f"Bearer {credentials.get_access_token().access_token}"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
now = datetime.now().strftime("%Y%m%d-%H%M%S")
full_name = f"dashboard_full_{now}.pdf"
name = f"dashboard_{now}.png"
with open(full_name, "wb") as f:
f.write(response.content)
images = convert_from_path(full_name, dpi=400)
images[0].save(name, 'PNG')
os.remove(full_name)
return name
return None