/// 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

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)")


twitter
Hatena
Google Buzz
newsing
Yahoo!
Buzzurl
Technorati
del.icio.us
Choix
Iza!
Livedoor Clip
Facebook
Evernote
 

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


PhotoGallery


TWITTER
Search

Mobile
QR for cellphone  QR for smart phone
For mobile click here
For smart phone click here
Popular Page
#1Web
#2Hiace 200
#3Gadget
#4The beginning of CSSレイアウト
#5Column
#6Web font test
#7Ora Ora Ora Ora Ora
#8Wifi cam
#9みたらし団子
#10Arcade Controller
#11G Suite
#12PC SPEC 2012.8
#13Javascript
#14REMIX DTM DAW - Acid
#15RSS Radio
#16Optimost
#17通話SIM
#18Attachment
#19Summer time blues
#20Enigma
#21Git
#22Warning!! Page Expired.
#23Speaker
#24Darwinian Theory Of Evolution
#25AV首相
#26htaccess mod_rewite
#27/// BANGBOO BLOG /// From 2016-01-01 To 2016-01-31
#28竹書房
#29F☆ck CSS
#30Automobile Inspection
#31No ID
#32Win7 / Win10 Insco
#33Speaker
#34Arcade Controller
#35Agile
#36G Suite
#37Personal Information Privacy Act
#38Europe
#39Warning!! Page Expired.
#40GoogleMap Moblile
#41CSS Selectors
#42MySQL DB Database
#43Ant
#44☆od damnit
#45Teeth Teeth
#46Itinerary with a eurail pass
#47PHP Developer
#48Affiliate
#49/// BANGBOO BLOG /// From 2019-01-01 To 2019-01-31
#50/// BANGBOO BLOG /// From 2019-09-01 To 2019-09-30
#51/// BANGBOO BLOG /// On 2020-03-01
#52/// BANGBOO BLOG /// On 2020-04-01
#53Windows env tips
#54恐慌からの脱出方法
#55MARUTAI
#56A Rainbow Between Clouds‏
#57ER
#58PDF in cellphone with microSD
#59DJ
#60ICOCA
#61Departures
#62Update your home page
#63CSS Grid
#64恐慌からの脱出方法
#65ハチロクカフェ
#66/// BANGBOO BLOG /// On 2016-03-31
#67/// BANGBOO BLOG /// From 2017-02-01 To 2017-02-28
#68/// BANGBOO BLOG /// From 2019-07-01 To 2019-07-31
#69/// BANGBOO BLOG /// From 2019-10-01 To 2019-10-31
#70/// BANGBOO BLOG /// On 2020-01-21
#71Bike
#72Where Hiphop lives!!
#73The team that always wins
#74Tora Tora Tora
#75Blog Ping
#76無料ストレージ
#77jQuery - write less, do more.
#78Adobe Premire6.0 (Guru R.I.P.)
#79PC SPEC 2007.7
#80Google Sitemap
#81Information privacy & antispam law
#82Wifi security camera with solar panel & small battery
#83Hope get back to normal
#84Vice versa
#85ハイエースのメンテ
#86Camoufla
#87α7Ⅱ
#88Jack up Hiace
#89Fucking tire
#90Big D
#914 Pole Plug
#925-year-old shit
#93Emancipation Proclamation
#94Windows env tips
#95Meritocracy
#96Focus zone
#97Raspberry Pi
#98Mind Control
#99Interview
#100Branding Excellent
Category
Recent Entry
Trackback
Comment
Archive
<     December 2024     >
Sun Mon Tue Wed Thi Fri Sat
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
Link