( 方法:files.copy )
请查看我的Python代码
get_ipython().system('pip install --upgrade google-api-python-client')
get_ipython().system('pip install google-auth==1.35.0')
get_ipython().system('pip install google-auth-oauthlib==0.4.4')
get_ipython().system('pip install googleapiclient.discovery')
get_ipython().system('googleapiclient.errors import HttpError')
get_ipython().system('pip install pandas')
import json
import pandas as pd
import csv
from googleapiclient.discovery import build
from google.oauth2 import service_account
# ## Download Pygsheets
import pygsheets
# ## Authenticating To A Google Cloud Project With A .JSON Service Account Key
# Using a previously obtained local .json file you can authenticate to your google service account.
with open('credentials.json') as source:
info = json.load(source)
credentials = service_account.Credentials.from_service_account_info(info)
# --------------------------------------------------------
# ## Set credentials and Templat Doc ID
credentials_path = '/Users/baker/Desktop/a3/credentials.json'
template_document_id = '12g6okE4Hvr5VCMiGo6Z7szNSp_cZIRafdLHafGdEtRs'
# ## Load credentials from the JSON file
credentials = service_account.Credentials.from_service_account_file( credentials_path, scopes=['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'] )
# ## Define constants and build API services
DOCS_FILE_ID = "12g6okE4Hvr5VCMiGo6Z7szNSp_cZIRafdLHafGdEtRs"
folder_id = '1IWTXGhYhP94MwUj601F7IO_R0ae7wTA7'
SOURCES = ('text', 'sheets')
SOURCE = 'sheets'
COLUMNS = ['to_name', 'to_title', 'to_company', 'to_address']
# ## Build API Services
DRIVE = build('drive', 'v2', credentials=credentials)
DOCS = build('docs', 'v1', credentials=credentials)
SHEETS = build('sheets', 'v4', credentials=credentials)
# ## Create A New Google Sheet + Obtain The Unique ID
# Firstly we are going to create a new google sheet, then we'll obtain the id of that specific google sheet which can be found within the URL:
# ![how to obtain the google sheet id](https://sempioneer.com/wp-content/uploads/2020/05/2_obtain_the_google_sheet_id.png)
# --------------------------------------------------------------------------------
# ## Google Sheet Wizardry With Python Commands
# ### Authenticating With Google Sheets With Pyghseets
# Let's see how to successfully authenticate to google sheets with our .json key
client = pygsheets.authorize(service_account_file='credentials.json')
# ---------------------------------------------------------------------------------
# ### How To Connect To a Specific Google Sheet
# Now that we've authenticated pygsheets with our google cloud project let's connect to a specific google sheet.
#
# In order to do this we will need to grab the URL link and share the spreadsheet.
spreadsheet_url = \
"https://docs.google.com/spreadsheets/d/1SORbxjVtFH4Z6B0kl8qpuGuYrITadhEynlyNQ-MZFOU/edit#gid=1319260213"
test = spreadsheet_url.split('/d/')
SHEETS_FILE_ID = test[1:][0].split('/edit')[0]
#open a spreadsheet
sheet = client.open_by_key(SHEETS_FILE_ID) #method 1 by selecting by ID Key
# ### How To Select A Specific Google Worksheet
# Let's select the automatically created worksheet called Sheet1:
wks = sheet.worksheet_by_title('FS - JD Salingers')
# ------------------------------------------------------------------------
# ### Accessing Rows & Columns Meta-data
# After uploading a dataframe into a google sheet, you can see how many columns and rows you have with:
#
# ~~~
#
# wks.cols # To view the number of columns
# wks.rows # To view the number of rows
#
# ~~~
print(wks.cols)
print(wks.rows)
# ### How To Get All Of The Google Sheet Values In A Python Format
# We can also get all of the values in a list of lists:
# In[43]:
all_values = wks.get_all_values()
all_values[0]
# In my case, notice how this has picked up all of the empty spaces that are located on the right hand side of the worksheet:
# In[ ]:
# A way to quickly remove the empty strings per list would be to do a <strong> nested list comprehension: </strong>
# In[44]:
[[item for item in row if item] for row in all_values]
# In[ ]:
# ------------------------------------------------
# ### How To Get Cell Ranges In Google Sheets With Python
# You can also extract specific ranges (columns and rows) similar to your excel functions:
# In[36]:
cell_range = wks.range('A1:z10',
returnas='matrix')
# In[37]:
cell_range
# ### How To Get A Single Row For Extracting The Column Headers
# We can get a single row with:
#
# ~~~
#
# wks.get_row(row, returnas='matrix', include_tailing_empty=False, **kwargs)
#
# ~~~
# In[45]:
headers = wks.get_row(1, include_tailing_empty=False)
# In[46]:
print(headers)
# ------------------------------------------------------------------------------------
# ### How To Extract A Single Column
# Sometimes you might want to select a specific column from your worksheet:
# In[47]:
wks.get_col(2)
# ### Directly insert the data from Google Sheets into the merged document
# Create a new Google Docs document based on the template document
new_doc_body = {
'title': 'JD Salinger Finished'
}
new_doc = DOCS.documents().create(body=new_doc_body).execute()
new_doc_id = new_doc['documentId']
# Create the requests list for batch updating the document
requests = []
# Iterate over each row and add the insertText request to the requests list
for row in all_values[1:]:
request = {
'insertText': {
'location': {
'index': 1
},
'text': f'{row[0]} - {row[1]} - {row[2]} - {row[3]}\n\n'
}
}
requests.append(request)
# Execute the requests to insert the content into the document
DOCS.documents().batchUpdate(documentId=new_doc_id, body={'requests': requests}).execute()
print("Content has been inserted into the new Google Docs document.")
print(f"New Google Docs document created: https://docs.google.com/document/d/{new_doc_id}")
预期行为
访问生成的链接(即链接不要求访问并直接指向生成的文档供我访问)以及将生成的邮件合并文件存放在我的文件夹中。
实际行为
Receive the following message:
Content has been inserted into the new Google Docs document.
New Google Docs document created: https://docs.google.com/document/d/17KXUPXZd1hxcWa4-oaEVzrKaFuCmRlcONtu_3Z_i4wk
从这个角度来看,我的代码看起来运行得很好,但是然后。我访问这些链接,它要求我请求访问权限,尽管我使用同一个 Gmail 帐户执行了前面的所有步骤(即使用同一个 Gmail 帐户构建了所有凭据等)。
看一下这里的代码和注释:
# ## Authenticating To A Google Cloud Project With A .JSON Service Account Key
# Using a previously obtained local .json file you can authenticate to your google service account.
with open('credentials.json') as source:
info = json.load(source)
credentials = service_account.Credentials.from_service_account_info(info)
您似乎正在使用服务帐户进行身份验证,因为文件是使用服务帐户作为身份验证方法创建的,文件的所有者很可能是服务帐户本身,如果您有 Google Workspace 帐户,则可以绕过通过使用模拟(确保首先为您的服务帐户设置域范围委派。)
如果您没有 Workspace 帐户,也许更改授权方法会更好,您可以通过 OAuth 2.0 作为特定用户进行身份验证,这确实需要用户更多参与,但它的优点是始终创建使用运行应用程序的用户的凭据编写文档,因为系统将提示他们登录。