Skip to main content
 首页 » 编程设计

python之使用 Python 从 Google 文档中删除资源

2025年05月04日18sky-heaven

我正在尝试使用此功能删除 Google 文档中的电子表格:

def f_DeleteResource(xls_name): 
  """Delete a resource""" 
  client=Auth() 
  for e1 in client.GetResources().entry: 
    e2 = client.GetResource(e1) 
    if xls_name==e2.title.text: 
      client.DeleteResource(e2.resource_id.text,True) 

当我更改 client.DeleteResource(p1,p2) 的第一个参数时,我得到了不同的错误:

client.DeleteResource(e2.resource_id.text,True):

Traceback (most recent call last): 
File "C:\xmp\D6GDocsDeleteUpload.py", line 164, in <module> main() 
File "C:\xmp\D6GDocsDeleteUpload.py", line 157, in main f_DeleteResource(sys.argv[2]) 
File "C:\xmp\D6GDocsDeleteUpload.py", line 144, in f_DeleteResource client.DeleteResource(e2.resource_id.text,True) 
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 540, in delete_resource uri = entry.GetEditLink().href 
AttributeError: 'str' object has no attribute 'GetEditLink' 

client.DeleteResource(e2,True):

Traceback (most recent call last): 
File "C:\xmp\D6GDocsDeleteUpload.py", line 164, in <module> main() 
File "C:\xmp\D6GDocsDeleteUpload.py", line 157, in main f_DeleteResource(sys.argv[2]) 
File "C:\xmp\D6GDocsDeleteUpload.py", line 144, in f_DeleteResource client.DeleteResource(e2,True) 
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 543, in delete_resource return super(DocsClient, self).delete(uri, **kwargs) 
File "C:\Python27\lib\site-packages\gdata\client.py", line 748, in delete **kwargs) 
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 66, in request return super(DocsClient, self).request(method=method, uri=uri, **kwargs) 
File "C:\Python27\lib\site-packages\gdata\client.py", line 319, in request RequestError) 
gdata.client.RequestError: Server responded with: 403, <errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>matchHeaderRequired</code><location type='header'>If-Match|If-None-Match</location><internalReason>If-Match or If-None-Match header or entry etag attribute required</internalReason></error></errors> 

谁能帮帮我?

请您参考如下方法:

这似乎是 Google API Python 库中的错误。我检查了 gdata-2.0.16 并注意到 DeleteResource() 函数仅使用资源的 URL(gdata/docs/client.py 第 540-543 行),但后来检查 hasattr(entry_or_uri, 'etag')(gdata/client.py 行 737-741),当然字符串值 (uri) 没有 etag 属性。

您可以使用 force 关键字参数来解决它:

import gdata.docs.data 
import gdata.docs.client 
 
client = gdata.docs.client.DocsClient() 
client.ClientLogin('xxxxxx@gmail.com', 'xxxxxx', 'XxX') 
 
for doc in client.GetAllResources(): 
    if doc.title.text == 'qpqpqpqpqpqp': 
        client.DeleteResource(doc, force=True) 
        break 

如果您愿意,您可以向库维护者报告错误(如果尚未报告)。