如何使用 Python 將渲染的 HTML 範本插入 Google 文件
我面臨一個挑戰,使用 python 以程式設計方式將 html 模板插入到 google 文件中。我知道 google 文檔編輯器或 google 文件 api 中沒有原生/內建功能可以解決我的問題,但我嘗試了一些技巧來達到我的目標。這裡我們忽略了我們應該插入文件中的“哪裡”,現在只要成功插入就足夠了。
我的方法是:
- 在 google 雲端硬碟上傳 html 文件,名稱為
application/vnd.google-apps.document
,因為 google 文件會自動將 html 轉換為文件。 (不完美,但有效) - 使用 google docs api get() 取得文件內容(google docs json 格式)。
- 使用 google 文件 batchupdate() 更新目標文件上的新內容。
def insert_template_to_file(target_file_id, content): media = MediaIoBaseUpload(BytesIO(content.encode('utf-8')), mimetype='text/html', resumable=True) body = { 'name': 'document_test_html', 'mimeType': 'application/vnd.google-apps.document', 'parents': [DOC_FOLDER_ID] } try: # Create HTML as docs because it automatically convert html to docs content_file = driver_service.files().create(body=body, media_body=media).execute() content_file_id = content_file.get('id') # Collect html content from Google Docs after created doc = docs_service.documents().get(documentId=content_file_id, fields='body').execute() request_content = doc.get('body').get('content') # Insert the content from html to target file result = docs_service.documents().batchUpdate(documentId=target_file_id, body={'requests': request_content}).execute() print(result) # Delete html docs driver_service.files().delete(fileId=content_file_id).execute() print("Content inserted successfuly") except HttpError as error: # Delete html docs even if failed driver_service.files().delete(fileId=content_file_id).execute() print(f"An error occurred: {error}")
問題是:我從步驟 2 收集的內容與batchupdate() 所需的內容不符。我正在嘗試調整步驟 2 中的內容以匹配步驟 3,但尚未成功。
目標解決方案:取得帶有 html 程式碼的字串,插入呈現的 html 到 google 文件上的目標檔案。目標是將目標檔案的現有內容附加到 html,而不是覆蓋。
我的方法有意義嗎?您還有其他想法可以實現我的目標嗎?
正確答案
我相信您的目標如下。
- 您希望透過呈現 html 將 html 資料附加到 google 文件。
- 您希望使用適用於 python 的 googleapis 來實現此目的。
遺憾的是,現階段「method:documents.get」檢索到的json物件似乎無法直接用作「method:documents.batchupdate」的請求體。
但是,如果您想將 html 附加到現有的 google 文件中,我認為僅使用 drive api 即可實現。當這反映在範例腳本中時,下面的範例腳本怎麼樣?
範例腳本:
def insert_template_to_file(target_file_id, content): request = drive_service.files().export(fileId=target_file_id, mimeType="text/html") file = BytesIO() downloader = MediaIoBaseDownload(file, request) done = False while done is False: status, done = downloader.next_chunk() print("Download %d%%" % int(status.progress() * 100)) file.seek(0) current_html = file.read() media = MediaIoBaseUpload(BytesIO(current_html + content.encode('utf-8')), mimetype='text/html', resumable=True) body = {'mimeType': 'application/vnd.google-apps.document'} try: result = drive_service.files().update(fileId=target_file_id, body=body, media_body=media).execute() print(result) print("Content inserted successfuly") except HttpError as error: print(f"An error occurred: {error}")
- 在此修改後的腳本中,從現有 google 文件中檢索 html 數據,並將新的 html 附加到檢索到的 html 中。並且,google 文件透過更新的 html 進行更新。在你的情況下,原始數據似乎是 html。所以我覺得這個方法或許可以用。
注意:
- 此腳本會覆寫
target_file_id
的 google 文件。因此,當您測試此腳本時,我建議您使用範例 google 文件。
參考文獻:
以上是如何使用 Python 將渲染的 HTML 範本插入 Google 文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優
