{"id":21261,"date":"2023-06-11T21:45:43","date_gmt":"2023-06-11T12:45:43","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=21261"},"modified":"2023-06-12T07:46:22","modified_gmt":"2023-06-11T22:46:22","slug":"%e3%80%90python%e3%80%91%e3%82%b9%e3%83%97%e3%83%ac%e3%83%83%e3%83%88%e3%82%b7%e3%83%bc%e3%83%88%e5%87%ba%e5%8a%9b","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=21261","title":{"rendered":"\u3010Python\u3011\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u64cd\u4f5c"},"content":{"rendered":"<h2>\u4e8b\u524d\u6e96\u5099<\/h2>\n<h3>1.Google Cloud Platform\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h3>\n<h4>\u4ee5\u4e0b\u306eURL\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/h4>\n<p>https:\/\/console.developers.google.com\/<\/p>\n<p>\u3082\u3057\u3001Google\u30a2\u30ab\u30a6\u30f3\u30c8\u3067\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u306a\u3044\u30b2\u30b9\u30c8\u72b6\u614b\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u30ed\u30b0\u30a4\u30f3\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h4>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h4>\n<ol>\n<li>\u300c\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af<\/li>\n<li>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u3092\u5165\u529b\uff08\u4f8b\uff1aMy Project Test\u306a\u3069\uff09<\/li>\n<li>\u300c\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<\/ol>\n<h3>2.Google Drive API\u3092\u6709\u52b9\u306b\u3059\u308b\u3002<\/h3>\n<ol>\n<li>\u30b5\u30a4\u30c9\u30d0\u30fc\u306b\u3042\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u691c\u7d22\u7a93\u306b\u300cDrive\u300d\u3068\u5165\u529b\u3057\u3066\u691c\u7d22\u3059\u308b\u3002<\/li>\n<li>\u300cGoogle Drive API\u300d\u304c\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u300c\u6709\u52b9\u306b\u3059\u308b\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<\/ol>\n<h3>3.Google Sheets API\u3092\u6709\u52b9\u306b\u3059\u308b\u3002<\/h3>\n<ol>\n<li>\u30b5\u30a4\u30c9\u30d0\u30fc\u306b\u3042\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u691c\u7d22\u7a93\u306b\u300cSheets\u300d\u3068\u5165\u529b\u3057\u3066\u691c\u7d22\u3059\u308b\u3002<\/li>\n<li>\u300cGoogle Sheets API\u300d\u304c\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u300c\u6709\u52b9\u306b\u3059\u308b\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<\/ol>\n<h3>4.\u5916\u90e8\u30a2\u30d7\u30ea\u304b\u3089\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/h3>\n<h4>\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h4>\n<ol>\n<li>\u30b5\u30a4\u30c9\u30d0\u30fc\u306b\u3042\u308b\u300c\u8a8d\u8a3c\u60c5\u5831\u300d\u3092\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u753b\u9762\u4e0a\u90e8\u306e\u300c\u8a8d\u8a3c\u60c5\u5831\u3092\u4f5c\u6210\u300d\u3092\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u300c\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u9078\u629e\u3059\u308b\u3002<\/li>\n<li>\u300c\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u540d\u300d\u3092\u5165\u529b\u3059\u308b\u3002\uff08\u4f8b\uff1asheets-test\uff09<\/li>\n<li>\u300c\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<li>\u300c\u3053\u306e\u30b5\u30fc\u30d3\u30b9 \u30a2\u30ab\u30a6\u30f3\u30c8\u306b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u8a31\u53ef\u3059\u308b\u300d\u306f\u300c\u7d9a\u884c\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n<li>\u305d\u306e\u307e\u307e\u300c\u5b8c\u4e86\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n<h4>\u79d8\u5bc6\u9375\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h4>\n<ol>\n<li>\u4f5c\u6210\u3057\u305f\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n<li>\u753b\u9762\u4e0a\u90e8\u306e\u300c\u30ad\u30fc\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<li>\u300c\u9375\u3092\u8ffd\u52a0\u300d\u2192\u300c\u65b0\u3057\u3044\u9375\u3092\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<li>\u30ad\u30fc\u306e\u30bf\u30a4\u30d7\u306b\u300cJSON\u300d\u304c\u3064\u3044\u305f\u72b6\u614b\u3067\u300c\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/li>\n<\/ol>\n<p>\u3053\u308c\u3067\u79d8\u5bc6\u9375\u306e\u751f\u6210\u306f\u5b8c\u4e86\u3067\u3059\u3002\u3053\u306e\u9375\u306f\u7121\u304f\u3055\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>5.\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u5074\u306e\u5171\u6709\u8a2d\u5b9a\u3092\u3059\u308b\u3002<\/h3>\n<ol>\n<li>\u8aad\u307f\u8fbc\u3080\u5bfe\u8c61\u306e\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u3092\u958b\u304d\u307e\u3059\u3002<\/li>\n<li>\u53f3\u4e0a\u306e\u300c\u5171\u6709\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n<li>\u300c\u30e6\u30fc\u30b6\u30fc\u3084\u30b0\u30eb\u30fc\u30d7\u3092\u8ffd\u52a0\u300d\u306b\u5bfe\u3057\u3066\u300c\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u300d\u3092\u30b3\u30d4\u30da\u3057\u307e\u3059\u3002<\/li>\n<li>\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306f\u5b9f\u969b\u306b\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u3042\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u306e\u3067\u300c\u901a\u77e5\u300d\u306e\u30c1\u30a7\u30c3\u30af\u306f\u5916\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/li>\n<li>\u300c\u9001\u4fe1\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n<h3>6.\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/h3>\n<pre class=\"lang:default decode:true\"> pip install gspread oauth2client<\/pre>\n<h4>gspread<\/h4>\n<p>\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u64cd\u4f5c\u306e\u305f\u3081\u306e\u30d1\u30c3\u30b1\u30fc\u30b8<\/p>\n<h4>oauth2client<\/h4>\n<p>\u8a8d\u8a3c\u60c5\u5831\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea(\u73fe\u5728\u306f\u975e\u63a8\u5968\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002)<\/p>\n<h2>\u8aad\u307f\u8fbc\u307f<\/h2>\n<p>\u30bb\u30eb\u300cA1\u300d\u306e\u5024\u3092\u6a19\u6e96\u51fa\u529b\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import gspread\r\nfrom oauth2client.service_account import ServiceAccountCredentials\r\n\r\n# json\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u3063\u3066\u8a8d\u8a3c\u60c5\u5831\u3092\u53d6\u5f97\r\nscope = ['https:\/\/spreadsheets.google.com\/feeds','https:\/\/www.googleapis.com\/auth\/drive']\r\nc = ServiceAccountCredentials.from_json_keyfile_name('\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u79d8\u5bc6\u9375json\u306e\u30d1\u30b9', scope)\r\n\r\n# \u8a8d\u8a3c\u60c5\u5831\u3092\u4f7f\u3063\u3066\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306e\u64cd\u4f5c\u6a29\u3092\u53d6\u5f97\r\ngs = gspread.authorize(c)\r\n\r\n# \u5171\u6709\u3057\u305f\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306e\u30ad\u30fc\uff08\u5f8c\u8ff0\uff09\u3092\u4f7f\u3063\u3066\u30b7\u30fc\u30c8\u306e\u60c5\u5831\u3092\u53d6\u5f97\r\nSPREADSHEET_KEY = '\u958b\u3044\u3066\u3044\u308b\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u306eURL\u306e\u300c\/d\/\u300d\u3068\u300c\/edit#gid=0\u300d\u306e\u9593\u306e\u9577\u3044\u6587\u5b57\u5217'\r\nworksheet = gs.open_by_key(SPREADSHEET_KEY).worksheet('\u30b7\u30fc\u30c8\u540d')\r\nprint(worksheet.acell('A1').value)<\/pre>\n<h3>pandas\u306eDataFrame\u306b\u3059\u308b\u3002<\/h3>\n<pre class=\"lang:default decode:true\">import pandas as pd\r\n# \u30b7\u30fc\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\nworkbook = gs.open_by_key(SPREADSHEET_KEY)\r\nworksheet = workbook.worksheet('\u30b7\u30fc\u30c8\u540d')\r\ndf = pd.DataFrame(worksheet.get_all_values())\r\nprint(df.head()) # \u30b7\u30fc\u30c8\u5185\u5bb9\u304cdataFrame\u306e\u5f62\u3067\u8aad\u307f\u53d6\u308c\u3066\u3044\u308b\u3002\r\n\r\n# 1\u884c\u76ee\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u53d6\u308b\u3002\r\ndf.columns = df.iloc[0]\r\nprint(df.head())\r\n\r\n# 1\u884c\u76ee\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u3002\r\ndf = df.drop(df.index[[0]])\r\nprint(df.head())\r\n\r\n# \u5404\u5217\u306e\u30c7\u30fc\u30bf\u578b\u3092\u78ba\u8a8d\r\nprint(df.dtypes)<\/pre>\n<h4>get_all_values<\/h4>\n<p>\u30b7\u30fc\u30c8\u5185\u306e\u5024\u3092\u591a\u6b21\u5143\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3002<\/p>\n<h2>\u66f8\u304d\u8fbc\u307f<\/h2>\n<h3>\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"prism line-numbers lang\" data-file=\"\" data-lang=\"\">pip install gspread_dataframe<\/pre>\n<h3>\u30b5\u30f3\u30d7\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">from gspread_dataframe import set_with_dataframe\r\n# \u30b7\u30fc\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\nworkbook = gs.open_by_key(SPREADSHEET_KEY)\r\nworkbook.add_worksheet(title='newSheet', rows=50, cols=10)\r\nset_with_dataframe(workbook.worksheet('newSheet'), df, include_index=True)<\/pre>\n<p>\u8a66\u3057\u306b\u300cnewSheet\u300d\u3068\u3044\u3046\u30b7\u30fc\u30c8\u3092\u4f5c\u3063\u3066\u4e0a\u306eDataFrame\u3092\u66f8\u304d\u51fa\u3057\u3066\u3044\u307f\u307e\u3059\u3002<\/p>\n<h4>workbook.add_worksheet(title='newSheet', rows=50, cols=10)<\/h4>\n<p>\u30b7\u30fc\u30c8\u3092\u30b9\u30d7\u30ec\u30c3\u30c8\u30b7\u30fc\u30c8\u306b\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002rows\u3084cols\u3092\u9818\u57df\u3092\u6c7a\u3081\u308c\u307e\u3059\u3002\uff08\u901a\u5e38\u306eExecel\u3068\u9055\u3063\u3066\u30b7\u30fc\u30c8\u306e\u30bb\u30eb\u7bc4\u56f2\u306e\u30b5\u30a4\u30ba\u3092\u6c7a\u3081\u308c\u307e\u3059\u3002\uff09<\/p>\n<h4>set_with_dataframe<\/h4>\n<p>\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u5185\u5bb9\u3092\u30ef\u30fc\u30af\u30b7\u30fc\u30c8\u306b\u53cd\u6620\u3059\u308b\u305f\u3081\u306e\u95a2\u6570\u3067\u3059\u3002<\/p>\n<h4>include_index<\/h4>\n<p>\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u756a\u53f7\u3092\u66f8\u304d\u51fa\u3059\u304b\u3069\u3046\u304b\u3067\u3059\u3002True\u306a\u3089\u66f8\u304d\u51fa\u3057\u307e\u3059\u3002<\/p>\n<div>\n<h3>\u30ef\u30fc\u30af\u30b7\u30fc\u30c8\u3092\u524a\u9664\u3059\u308b\u3002<\/h3>\n<div>\n<pre class=\"lang:default decode:true \">workbook.del_worksheet(workbook.worksheet('newSheet'))<\/pre>\n<p>\u518d\u5ea6add_worksheet\u3092\u5b9f\u884c\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u518d\u5ea6\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408\u306f\u4e0a\u8a18\u95a2\u6570\u3067\u524a\u9664\u3057\u307e\u3059\u3002<\/p>\n<\/div>\n<div><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e8b\u524d\u6e96\u5099 1.Google Cloud Platform\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002 \u4ee5\u4e0b\u306eURL\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002 https:\/\/console.developers.google.com\/ \u3082\u3057\u3001Google\u30a2\u30ab\u30a6\u30f3 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[47],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21261"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21261"}],"version-history":[{"count":9,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21261\/revisions"}],"predecessor-version":[{"id":21270,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21261\/revisions\/21270"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21261"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}