SeaTable 提供了 API get_file_download_link(path)
来下载表中的文件和图片。这里的主要问题是怎么从文件单元格和图片单元格中的内容来获取 path 值。下面来具体说明
一个文件单元格的格式如下
[ {'name': 'orders-2020.9-2.pdf',
'size': 7745408,
'type': 'file',
'url': 'https://cloud.seatable.cn/workspace/24193/asset/65ed2248-9134-4500-8848-ad430ec70bed/files/2020-09/%E5%BA%9F%E6%B0%94%E6%A3%80%E6%B5%8B2020.9-2.pdf'} ]
对文件单元格,我们可以用以下的方式来取的 path 列表:
from urllib import parse
files = row.get('文件列名'), [])
urls = [file.get('url') for file in files]
paths = []
for url in urls:
path = parse.unquote(url[url.find('/files/'):])
paths.append(path)
一个图片单元格的格式如下
['https://cloud.seatable.cn/workspace/24193/asset/65ed2248-9134-4500-8848-ad430ec70bed/images/2020-09/image%20(9).png', 'https://cloud.seatable.cn/workspace/24193/asset/65ed2248-9134-4500-8848-ad430ec70bed/images/2020-09/image%20(10).png']
对图片单元格,我们可以用以下的方式来取的 path 列表:
from urllib import parse
images = row.get('图片列名'), [])
urls = [image.get('url') for image in images]
paths = []
for url in urls:
path = parse.unquote(url[url.find('/images/'):])
paths.append(path)
有了路径,我们就可以调用 get_file_download_link(path)
来下载文件和图片了:
file_url = base.get_file_download_link(path)
file_name = parse.unquote(file_url.split('/')[-1])
response = requests.get(file_url)
with open(os.path.join(dir_name, file_name), 'wb') as f:
f.write(response.content)
完整的例子可以参考: