你好,我正在尝试更新一个项目列表到谷歌工作表。
示例我有一个csv文件data.csv,如下所示
Item Name,Qty,Price,Store,Location,Stock
Apple,50,50,Store-1,LA,1000
Mango,30,100,Store-2,SL,5000
Bananna,10,10,Store-3,NY,4000我的Python文件代码是:
client = gspread.service_account_from_dict(credentials)
sheetName = "Sheet1"
obj = {}
with open('data.csv') as f:
r = csv.reader(f)
next(f)
for row in r:
obj[row[0]] = row[1:6]
spreadsheet = client.open("CSV-to-Google-Sheet")
sheet = spreadsheet.worksheet(sheetName)
values = sheet.get_all_values()[1:]
res = [obj.get(row[0]) if obj.get(row[0]) else ["", ""] for row in values]
sheet.update("B2", res, value_input_option="USER_ENTERED")所以每当我们运行脚本时,
它应该检查苹果是否存在于Google sheet Column 1 Item Name 中,然后它应该开始填充剩余的列

Qty到Qty列
价格到价格栏
存储到存储列
定位到位置列
股票到股票列
发布于 2022-07-05 19:18:45
您可以使用以下方法将当前的google工作表数据放入数据中:
import pygsheets
gc = pygsheets.authorize(service_file=service_file)
sh = gc.open("Sheet1")
wks = sh.sheet1
old_data = pd.DataFrame(wks.get_all_records())然后,您可以在dataframe上做任何想要的检查,并使用以下方法更新google工作表。
df = new_data # your new dataset (dataframe)
found = old_data[old_data['Item Name'].str.contains('Apple')]
if found.count() > 0:
wks.set_dataframe(df, (1, 1))https://stackoverflow.com/questions/72874370
复制相似问题