Use Python API to download files and pictures in a table

SeaTable provides API get_file_download_link(path) to download files and pictures in the table. The main problem here is how to get the path value from the contents of the file cell and the image cell. Let’s explain in detail below.

The format of a file cell is as follows

[ {'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'} ]

For file cells, we can get the path list in the following way:

from urllib import parse
files = row.get('file-column-name'), [])
urls = [file.get('url') for file in files]
paths = []
for url in urls:
  path = parse.unquote(url[url.find('/files/'):])
  paths.append(path)

The format of an image cell is as follows

['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']

For image cells, we can use the following ways to get the path list:

from urllib import parse
images = row.get('image-column-name'), [])
urls = [image.get('url') for image in images]
paths = []
for url in urls:
  path = parse.unquote(url[url.find('/images/'):])
  paths.append(path)

With the path, we can call get_file_download_link(path) to download files and images:

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)

The complete example can refer to:

1 Like