-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathautomate.py
More file actions
61 lines (51 loc) · 1.65 KB
/
automate.py
File metadata and controls
61 lines (51 loc) · 1.65 KB
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
import mysql.connector
from save_to_spreadsheet import authenticate, create_tab, write_data
import datetime
def getSheetName():
# TO-DO (Optional)
# Remove below lines and write your own logic to create a sheet with different name
now = datetime.datetime.now()
today = now.strftime("%d %b %Y")
return today
def getData():
# TO-DO
# Enter your database connection details here
# Keep the ssl_disabled = "True" if you are facing any issue
sh_db = mysql.connector.connect(
host="",
user="",
passwd="",
database="",
ssl_disabled="True",
)
db_cursor = sh_db.cursor()
# TO-DO
# Paste your query here
# Replace YOUR QUERY GOES HERE with your query, DO NOT REMOVE THE """ """
db_cursor.execute(""" YOUR QUERY GOES HERE """)
# TO-DO
# Enter your column names as 2-d list
# Replace column name 1, 2 etc with your column names
# These names will be saved as column titles in your Google Sheet
data = [
[
"Column name 1",
"Column name 2",
"Column name 3"
]
]
result = db_cursor.fetchall()
data += result
return data
def main():
# TO-DO
# Enter the ID of the Google Spreadsheet in which we are going to save the data
# Make sure that the authenticated account has access to this Google Sheet
SPREADSHEET_ID = "ID OF YOUR SPREAD SHEET (GET IT FROM URL)"
SHEET_NAME = getSheetName()
SERVICE = authenticate()
DATA = getData()
create_tab(SERVICE, SPREADSHEET_ID, SHEET_NAME)
write_data(SERVICE, SPREADSHEET_ID, SHEET_NAME, DATA)
if __name__ == "__main__":
main()