get_spread.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/python3
  2. import sys
  3. import codecs
  4. import traceback
  5. import redis
  6. import requests
  7. import re
  8. import pandas as pd
  9. import random
  10. import urllib
  11. import json
  12. import gspread
  13. import datetime
  14. from gspread_pandas import Spread, Client
  15. from oauth2client.service_account import ServiceAccountCredentials
  16. import os
  17. from itertools import islice
  18. def save_sheet(df,filename,tabname,startpos='A1'):
  19. scope = ['https://spreadsheets.google.com/feeds',
  20. 'https://www.googleapis.com/auth/drive']
  21. credentials = ServiceAccountCredentials.from_json_keyfile_name('/Users/zooeytsai/kw_tools/INNNews/innnews-d27b01472ba2.json', scope)
  22. gc = gspread.authorize(credentials)
  23. spread = Spread(filename,creds=credentials)
  24. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
  25. def get_sheets(filename):
  26. result=[]
  27. scope = ['https://spreadsheets.google.com/feeds',
  28. 'https://www.googleapis.com/auth/drive']
  29. credentials = ServiceAccountCredentials.from_json_keyfile_name('/Users/zooeytsai/kw_tools/INNNews/innnews-d27b01472ba2.json', scope)
  30. gc = gspread.authorize(credentials)
  31. # files=gc.list_spreadsheet_files()
  32. # print(files)
  33. spread = Spread(filename,creds=credentials)
  34. df=spread.sheet_to_df(sheet='2022')
  35. for idx, row in islice(df.iterrows(), 1, None):
  36. r1=row[1].strip()
  37. if len(r1)>1:
  38. r2=row[2].strip()
  39. r3=row[3].strip()
  40. if "病假" not in r1:
  41. result.append({'kw':r1+" "+r2+" "+r3})
  42. else:
  43. pass
  44. return result
  45. result=get_sheets('引新聞INNEWS稿單xSEO優化')
  46. js=json.dumps(result, ensure_ascii=False)
  47. print(js)
  48. # r = redis.Redis(host='db.ptt.cx', port=6379, db=1,password='choozmo9')
  49. # r.set('innews_sheet',js)
  50. # spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
  51. #