Last active
December 20, 2022 18:24
-
-
Save mintommm/83f9b8e9499358f9bf006399e51ba50f to your computer and use it in GitHub Desktop.
Googleスライドでベクター画像を扱うためのアップローダー.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"toc_visible": true, | |
"name": "Googleスライドでベクター画像を扱うためのアップローダー.ipynb", | |
"private_outputs": true, | |
"collapsed_sections": [ | |
"kz8_uzhRiqjh" | |
], | |
"authorship_tag": "ABX9TyP/HY3/EUkXkGeEutfgwZUx", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"gpuClass": "standard" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/mintommm/83f9b8e9499358f9bf006399e51ba50f/google.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 解説\n", | |
"\n", | |
"https://zenn.dev/mintommm/articles/insert-vector-img-into-googleslides" | |
], | |
"metadata": { | |
"id": "20vGuphOxe4c" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## ライブラリの読み込み" | |
], | |
"metadata": { | |
"id": "yflMDOxigv60" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from google.colab import auth, files\n", | |
"from googleapiclient.discovery import build\n", | |
"from googleapiclient.http import MediaFileUpload" | |
], | |
"metadata": { | |
"id": "Px5Nq4OUgtVY" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 認証情報の設定\n", | |
"\n", | |
"ランタイムごとに初回のみ必要 \\\n", | |
"複数回実行しても問題ない" | |
], | |
"metadata": { | |
"id": "YKArWM2uhNOH" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"auth.authenticate_user()" | |
], | |
"metadata": { | |
"id": "f5USd7OAhL6i" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Colab実行環境へファイルをアップロード\n", | |
"\n", | |
"同名のファイルを既にアップロードしている場合はランタイムの接続解除で初期化するのが楽 \\\n", | |
"※このコードでは1件のみ扱う場合を想定している" | |
], | |
"metadata": { | |
"id": "YmOV2xD5gzUL" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"uploaded = files.upload()" | |
], | |
"metadata": { | |
"id": "dvkCezFShJRV" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## ファイルのアップロード実行\n", | |
"\n", | |
"このコードでは、ファイルはマイドライブ直下にアップロードされる" | |
], | |
"metadata": { | |
"id": "hbBsbcHAhgLR" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"service = build('drive', 'v3')\n", | |
"\n", | |
"file_metadata = {\n", | |
" 'name': list(uploaded.keys())[0],\n", | |
" 'mimeType': 'application/x-msmetafile' #この指定によりGoogle図形描画で開くことができるようになる\n", | |
"}\n", | |
"\n", | |
"media = MediaFileUpload(list(uploaded.keys())[0], mimetype='application/x-msmetafile')\n", | |
"\n", | |
"service.files().create(body=file_metadata, media_body=media, fields='id').execute()" | |
], | |
"metadata": { | |
"id": "kBj1Dgj0hd9p" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Appendix" | |
], | |
"metadata": { | |
"id": "kz8_uzhRiqjh" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 現在のランタイム上のファイルを確認" | |
], | |
"metadata": { | |
"id": "mgWMmwsPhjDH" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!ls" | |
], | |
"metadata": { | |
"id": "OaP3gu0tboPS" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 参考\n", | |
"\n", | |
"https://colab.research.google.com/notebooks/io.ipynb\n", | |
"\n", | |
"https://developers.google.com/drive/api/guides/manage-uploads#multipart" | |
], | |
"metadata": { | |
"id": "q-UO2_3nnIrl" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment