如何使用 Python link 将 Google 表单转换为 Google Sheet?

How to link a Google Form to a Google Sheet using Python?

这是我的设置和我想要完成的:

这是问题所在:

每当我这样做时,它都会终止 Google Sheet 和 Google 表单之间的 link。有什么方法可以保留此 link 或重新 link 表单和 Sheet?

这是我一直在玩的代码片段:

submissions_data = drive.CreateFile({'id': ext['SUBMISSIONS_ID']})
submissions_data.GetContentFile("data.csv", mimetype = "text/csv")
df = pd.read_csv("data.csv")
df.drop([0],inplace=True)
df.to_csv("data.csv", index=False)
submissions_data.SetContentFile("data.csv")`

最好,我想找到一种使用 PyDrive 来完成此操作的方法,但此时任何方法都可以。任何帮助表示赞赏!谢谢!

使用 Google Drive API 与 oauth2clientgspread 直接更新现有电子表格。

  1. 转到Google APIs Console。创建一个新项目。点击启用 API.
  2. 启用Google Drive API。创建凭据 用于 Web 服务器访问应用程序数据。
  3. 命名服务帐户并授予其编辑者的项目角色。
  4. 下载 JSON 文件。
  5. 将JSON文件复制到你的代码目录,并将其重命名为 client_secret.json
  6. 找到 client_secret.json 里面的 client_email。返回您的电子表格,单击右上角的“共享”按钮,并将客户电子邮件粘贴到“人员”字段中以授予其编辑权限。点击发送。
pip install gspread oauth2client
#spreadsheet.py
import gspread
from oauth2client.service_account import ServiceAccountCredentials


# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds =   ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Copy of Legislators 2017").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

您可以通过更改特定单元格写入电子表格:

sheet.update_cell(1, 1, "I just wrote to a spreadsheet using Python!")

或者您可以在电子表格中插入一行:

row = ["I'm","inserting","a","row","into","a,","Spreadsheet","with","Python"]
index = 1
sheet.insert_row(row, index)

查看 gspread API reference 以了解有关这些功能以及其他几十个功能的完整详细信息。