【问题标题】:Python - How to read specific range of rows and columns from Google Sheet in Python?Python - 如何在 Python 中从 Google 表格中读取特定范围的行和列?
【发布时间】:2022-01-21 10:15:30
【问题描述】:

在 Google Sheet 中有类似下面的数据

需要读取从 'A4 到 C4' 列开始的数据范围,在 Python 中固定在下面无数行(灵活)。 帮帮我,因为我是这个使用 Python 的 Google Sheet 的新手。

Python 中作为 Dataframe df 的预期输出如下:

Name    Row Class
AA      1   II
BB      2   I
CC      3   III
DD      4   II

【问题讨论】:

    标签: python dataframe google-sheets rows


    【解决方案1】:

    在你的情况下,下面的示例脚本怎么样?

    示例脚本 1:

    如果您的电子表格没有作为 Web 发布方式发布,那么下面的脚本呢?在这个示例脚本中,我使用了 googleapis for python。所以,关于如何使用它,请查看Python Quickstart for Sheets API。我提议的脚本中的service = build('sheets', 'v4', credentials=creds)Python Quickstart for Sheets API 的脚本相同。

    spreadsheet_id = "###" # Please set the Spreadsheet ID.
    range_a1Notation = "Sheet1!A4:C" # Please set the range as the A1Notation.
    
    service = build('sheets', 'v4', credentials=creds)
    sheet = service.spreadsheets()
    result = sheet.values().get(spreadsheetId=spreadsheet_id, range=range_a1Notation).execute()
    values = result.get("values", [])
    df = pd.DataFrame(values)
    

    示例脚本 2:

    如果您的电子表格发布为 Web 发布,您可以使用以下脚本。

    import io
    import pandas as pd
    import requests
    
    url = 'https://docs.google.com/spreadsheets/d/e/2PACX-###/pub?sheet=Sheet1&range=A4%3AC&output=csv'
    df = pd.read_csv(io.BytesIO(requests.get(url).content), sep=',')
    
    • 在这种情况下,请将 2PACX-### 替换为您在 Web 上发布的 URL。
    • Sheet1sheet=Sheet1 是工作表名称。
    • A4%3ACrange=A4%3AC 是范围 A4:C 作为 A1Notation。

    参考资料:

    【讨论】:

    • 感谢您的意见。已尝试您的示例脚本 1,出现类似“无法解析范围:设置:B143”的错误。在这里,“设置”是我的工作表名称。不知道我哪里出错了。
    • 对示例脚本 1 的小改动(如前所述)对我有用! sheet_name = 'Settings' range_a1Notation = sheet_name + '!B143:D' 感谢您的帮助!
    • @user12345 感谢您的回复。很高兴您的问题得到解决。
    【解决方案2】:

    你可以像这样使用 pandas skiprows 我已经检查过了,它正在工作我正在使用 .ods

    import pandas as pd
    
    df = pd.read_excel(
        "test.ods", engine="odf", 
        index_col=None, 
        header=None, 
        skiprows=lambda x: x in [0,1,2],
        keep_default_na=False
    )
    print(df)
    

    输出
    https://imgur.com/VOQh4s7

    for i in df:
        print(df[i])
    

    输出
    https://imgur.com/8tzsh8F

    我知道这不是一个完整的解决方案,但几乎没有。

    【讨论】:

      【解决方案3】:

      使用openpyxl 很简单:

      from openpyxl import load_workbook
      
      wb = load_workbook(filename = 'your_path_to_file.xlsx')
      
      sheet = wb['Your sheet name']
          for index, row in enumerate(sheet.iter_rows()):
              if index == "your specific index":
                  "Do something"
      

      docs

      【讨论】:

        猜你喜欢
        • 2017-05-25
        • 1970-01-01
        • 2021-08-22
        • 2015-06-22
        • 1970-01-01
        • 2020-02-29
        • 2020-06-14
        • 1970-01-01
        • 2016-06-06
        相关资源
        最近更新 更多