我遇到下一个问题。我在Python中有一个数据框,并将其放在Google Spreadsheet中,并在数字列中得到了它:
我希望数字列采用数字格式。我一直在尝试,但是什么也没发生:
from gspread_formatting import *
fmt = cellFormat(numberFormat=numberFormat(type='NUMBER', pattern='####.#'))
format_cell_range(worksheet, 'Q2:R2', fmt)
如果我在fmt中放一个随机文本格式(如背景色),它会起作用...所以,我不知道为什么对numberFormat的转换不起作用...
在Python数据框中,这些列为数字格式:
谢谢!
编辑 这是将数据框从Python放到Google工作表的代码:
import df2gspread as d2g
def spreadsheet_first_paste(file_name, sheet_id,sheet_name, df):
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('...path/client_secret.json',scope)
gc = gspread.service_account(filename='...path/client_secret.json')
sh = gc.open(file_name)
spreadsheet_key = sheet_id
wks_name = sheet_name
d2g.upload(df, spreadsheet_key, wks_name, credentials=creds, row_names=True)
EDIT2 解决了: Problem with data format while Importing pandas DF from python into google sheets using df2gsheets 在那篇文章中是解决方案:我正在使用df2gspread库,所以我去了df2gspread.py并将行“ wks.update_cells(cell_list)”修改为“ wks.update_cells(cell_list,value_input_option ='USER_ENTERED')” / p>