0

The code I have is:

import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
from Google import Create_Service

def Create_Service(client_secret_file, api_name, api_version, *scopes):
    print(client_secret_file, api_name, api_version, scopes, sep='-')
    CLIENT_SECRET_FILE = client_secret_file
    API_SERVICE_NAME = api_name
    API_VERSION = api_version
    SCOPES = [scope for scope in scopes[0]]
    print(SCOPES)

    credencial = ServiceAccountCredentials.from_json_keyfile_name(CLIENT_SECRET_FILE, SCOPES)
    client = gspread.authorize(credencial)

    return client

def Upload_pandas(df, gsheet):
    tabela = df.reset_index()
    gsheet.sheet1.update([tabela.columns.values.tolist()] + tabela.values.tolist())
    return

CLIENT_SECRET_FILE = 'XXXXXX.json'
API_SERVICE_NAME = 'sheets'
API_VERSION = 'v4'
SCOPES = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

gsheetId = 'https://docs.google.com/spreadsheets/d/YYYYYYY/edit#gid=0'

cliente = Create_Service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES)

gsht = cliente.open_by_url(gsheetId)

I have an Excel file ("C:/Arquivo.xlsx") and want to upload it to the Google Sheets instead of convert the File to Dataframe and upload it, because I lose all formatation, like conditional_format etc. This should be like a Ctrl+C in the whole worksheets and Crtl+V in the Google Sheets document. Is there a way to do it using gspread? If doesn´t, which library could I use and how?

Thanks for any support!

  • Please ignore the line: 'from Google import Create_Service' Because I already wrote the function in the code – pedro chaves May 29 '20 at 12:04
  • There is no conditional formatting yet supported by other modules such as **xlrd** or **csv**. Your best option would be pandas, but in their have a look at your encoding. if you are lossing your data it straight away means that your reading encoding is different, such as "cp1252" etc encodings while reading the file. – Shafay May 29 '20 at 12:08
  • Let me see if I get it. You mean that I have to read the file with a different encode, so I imagine that would be something like: import pandas as pd data = pd.read_excel(r"C:/Arquivo.xlsx", ?) ------------------------------------------------- Which attribute of pandas.read_excel() are we talking about to define the reading encode? – pedro chaves May 29 '20 at 14:22
  • You can check this question that uses PyDrive to upload the file: https://stackoverflow.com/questions/53739061/upload-and-convert-xlsx-to-google-sheets-with-pydrive – Burnash Jun 02 '20 at 07:47

0 Answers0