/// BANGBOO BLOG ///

  • 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


June 1, 2023 List
GCP Python Google doc編集 on Jun 01, 2023 12:00 AM

June 1, 2023

GCP Python Google doc編集
Google Docのコピーや編集
https://developers.google.com/docs/api/how-tos/documents?hl=ja
https://rimever.hatenablog.com/entry/2019/10/16/060000
クイックスタート
https://developers.google.com/docs/api/quickstart/python?hl=ja
スコープ情蝣? https://developers.google.com/identity/protocols/oauth2/scopes?hl=ja#docs
ディスカバ繝? ドキュメント
例えばこれ縺?DriveAPIの分だが、RESTAPIで臀??ができるか全鐔??載しているっぽい
https://www.googleapis.com/discovery/v1/apis/drive/v3/rest
Drive API
https://developers.google.com/drive/api/guides/about-sdk?hl=ja
https://developers.google.com/drive/api/reference/rest/v3?hl=ja
Docs API
https://developers.google.com/docs/api/concepts/document?hl=ja
https://developers.google.com/docs/api/reference/rest?hl=ja
https://googleapis.github.io/google-api-python-client/docs/epy/index.html
https://googleapis.github.io/google-api-python-client/docs/dyn/docs_v1.html
https://developers.google.com/docs/api/reference/rest/v1/documents/get?hl=ja

文字置觸?? https://developers.google.com/docs/api/how-tos/merge?hl=ja

窶?DocAPIからdriveld folderidは藹??得できなさそう、getは使えそう
窶?DriveAPIが使えない?
コピーでな縺?DocAPI縺?get body からの新鐔??createで鐔??縺??
共有ドライブ時は、supports All Drives=True が必要だった縺?OK
file_metadata = service.files().get(fileld=DOCUMENT_ID, fields=id, name, mimeType, driveld', supports AllDrives=True) execute()

サービスアカウント縺?GWSにアクセスするに縺?GWS OU設藹??等が必要な場合がある>Google一般共有Docで觸??証も藹??

あるGoogle Docをコピーし、
本文を編集した上縺?
 本文の編集縺?((sample))となっている文字列をAAAに置觸??する
特藹??のドライブのフォルダに移動

from google.oauth2.service_account import Credentials
from googleapiclient.discovery
import build import re
#1. サービスアカウントの鐔??証情報を設藹??
SCOPES = ['https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = 'path/to/your/service-account-file.json' #サービスアカウント 縺?JSONファイルのパ繧?

creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

#2. Google Docs 縺? Driveのサービスをビルド
docs_service = build('docs', 'v1', credentials=creds)
drive_service = build('drive', 'v3', credentials=creds)

#3. コピー元縺?Google Doc縺?IDと、移動先のフォルダIDを設藹??
SOURCE_DOCUMENT_ID = 'source_doc_id' #コピー元のドキュメントID
TARGET_FOLDER_ID = 'target_folder_id' #移動先のフォルダID

#4. Google Docをコピ繝?
copied_doc = drive_service.files().copy(fileld=SOURCE_DOCUMENT_ID, body={"name": "Copied Document"), supportsAllDrives=True).execute()
copied_doc_id = copied_doc['id']

#5、本文を藹??得し、{{sample}} をAAAに置觸??
def replace_text(document_id, old_text, new_text)
#ドキュメントの内容を藹??得
document = docs_service.documents().get(documentid=document_id).execute()
content = document.get('body').get('content')
#リクエストリスト
requests = []
#検索と置觸??を行う
for element in content:
if 'paragraph' in element:
for paragraph_element in element['paragraph']['elements']:
if 'textRun' in paragraph_element:
text = paragraph_element['textRun']['content']
if old_text in text:
start_index = paragraph_element('startindex']
end_index = paragraph_element['endIndex']
requests append({
'replaceAllText': {
'containsText': {
'text': re.escape(old_text), #エスケープなしにする必要有
'matchCase': True
},
'replaceText': new_text
}
})
#置觸??リクエストを実行
if requests:
docs_service.documents().batchUpdate(documentid=document_id, body={'requests':requests}).execute()

#置觸??処理の藹??行
replace_text(copied_doc_id, '((sample))', 'AAA')

#6、コピーしたドキュメントを指定のフォルダに移動
drive_service.files().update(fileld=copied_doc_id, addParents=TARGET_FOLDER_ID, removeParents=copied doc['parents'][0], supportsAllDrives=True).execute() #親が藹??れないのでフォルダは繝?ードコード

print(f"Document copied, edited, and moved successfully! Document ID: {copied_doc_id)")


Posted by funa : 12:00 AM | Web | Comment (0) | Trackback (0)