1

I got the next problem. I have a dataframe in Python and I putted it in a Google Spreadsheet and I got this for the numeric columns:

enter image description here

I want the numeric columns in a numeric format. I've been trying this but nothing happens:

from gspread_formatting import *

fmt = cellFormat(numberFormat=numberFormat(type='NUMBER', pattern='####.#'))
format_cell_range(worksheet, 'Q2:R2', fmt)

If I put a random text format inside fmt (like background color), it works...So, I don´t know why the transform to numberFormat isn´t working...

In the Python dataframe, these columns are in a numeric format:

enter image description here

Thanks!

EDIT This is the code of put the dataframe from Python to a Google sheet:

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 SOLVED: Problem with data format while Importing pandas DF from python into google sheets using df2gsheets In that post was the solution: I was using the df2gspread library, so I went to the df2gspread.py and modified the lines "wks.update_cells(cell_list)" to "wks.update_cells(cell_list, value_input_option='USER_ENTERED')"

Silvio Aranda
  • 37
  • 1
  • 8
  • In your situation, can I ask you about the script for putting to the values from dataframe? Because when the values are put to the Spreadsheet, when `value_input_option='USER_ENTERED'` is used, the number is put as the number. But at gspread, the default value of `value_input_option` is `RAW`. I thought that by this, your issue might occur. How about this? – Tanaike Oct 08 '20 at 23:00
  • Hi Tanaike, I edited the post with the script! – Silvio Aranda Oct 09 '20 at 14:09
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike Oct 09 '20 at 23:07

1 Answers1

1

This solution really worked. Thanks for posting the solution: SOLVED: I was using the df2gspread library, so I went to the df2gspread.py and modified the lines "wks.update_cells(cell_list)" to "wks.update_cells(cell_list, value_input_option='USER_ENTERED')"

python gspread

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 05 '22 at 06:45