1

我不明白如何导入 Smartsheet 并将其转换为 pandas 数据框。我想操作来自 smartsheets 的数据,目前我将 smartsheets 导出到 csv 并在 python 中导入 csv 但想消除这一步,以便它可以按计划运行。

import smartsheet
import pandas as pd

access_token ='#################'

smartsheet = Smartsheet(access_token)
sheet = smartsheet.sheets.get('Sheet 1')
pd.DataFrame(sheet)
4

3 回答 3

5

这是将工作表转换为数据框的简单方法:

def simple_sheet_to_dataframe(sheet):
    col_names = [col.title for col in sheet.columns]
    rows = []
    for row in sheet.rows:
        cells = []
        for cell in row.cells:
            cells.append(cell.value)
        rows.append(cells)
    data_frame = pd.DataFrame(rows, columns=col_names)
    return data_frame

从智能表创建数据框的唯一问题是对于某些列类型cell.value并且cell.display_value是不同的。例如,联系人列将显示姓名或电子邮件地址,具体取决于使用的名称。

这是我需要将数据从 Smartsheet 拉入 Pandas 时使用的片段。请注意,我已经包含了垃圾收集,因为我经常处理数十张或接近 200,000 个单元格限制的工作表。

import smartsheet
import pandas as pd
import gc

configs = {'api_key': 0000000,
           'value_cols': ['Assigned User']}

class SmartsheetConnector:
    def __init__(self, configs):
        self._cfg = configs
        self.ss = smartsheet.Smartsheet(self._cfg['api_key'])
        self.ss.errors_as_exceptions(True)

    def get_sheet_as_dataframe(self, sheet_id):
        sheet = self.ss.Sheets.get_sheet(sheet_id)
        col_map = {col.id: col.title for col in sheet.columns}
        # rows = sheet id, row id, cell values or display values
        data_frame = pd.DataFrame([[sheet.id, row.id] +
                                   [cell.value if col_map[cell.column_id] in self._cfg['value_cols']
                                    else cell.display_value for cell in row.cells]
                                   for row in sheet.rows],
                                  columns=['Sheet ID', 'Row ID'] +
                                          [col.title for col in sheet.columns])
        del sheet, col_map
        gc.collect()  # force garbage collection
        return data_frame

    def get_report_as_dataframe(self, report_id):
        rprt = self.ss.Reports.get_report(report_id, page_size=0)
        page_count = int(rprt.total_row_count/10000) + 1
        col_map = {col.virtual_id: col.title for col in rprt.columns}
        data = []
        for page in range(1, page_count + 1):
            rprt = self.ss.Reports.get_report(report_id, page_size=10000, page=page)
            data += [[row.sheet_id, row.id] +
                     [cell.value if col_map[cell.virtual_column_id] in self._cfg['value_cols']
                      else cell.display_value for cell in row.cells] for row in rprt.rows]
            del rprt
        data_frame = pd.DataFrame(data, columns=['Sheet ID', 'Row ID']+list(col_map.values()))
        del col_map, page_count, data
        gc.collect()
        return data_frame

这为工作表和行 ID 添加了额外的列,以便我稍后可以在需要时写回 Smartsheet。

于 2019-12-18T14:55:15.787 回答
1

正如您在示例代码中所示,无法按名称检索表格。您完全有可能拥有多个具有相同名称的工作表。您必须使用他们的号码检索它们sheetId

例如:

 sheet = smartsheet_client.Sheets.get_sheet(4583173393803140)  # sheet_id

http://smartsheet-platform.github.io/api-docs/#get-sheet

Smartsheet 工作表有很多与之相关的属性。您需要遍历工作表的行和列来检索您要查找的信息,并以其他系统可以识别的格式构建它。

API 文档包含属性和示例列表。作为一个最小的例子:

for row in sheet.rows:
    for cell in row.cells
        # Do something with cell.object_value here
于 2019-12-06T19:29:14.057 回答
0

将工作表作为 csv 获取:(https://smartsheet-platform.github.io/api-docs/?python#get-sheet-as-excel-pdf-csv

smartsheet_client.Sheets.get_sheet_as_csv(
  1531988831168388,           # sheet_id
  download_directory_path)

将 csv 读入 DataFrame:(https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

pandas.read_csv
于 2021-01-27T05:26:10.683 回答