I am attempting to import data from Google Sheets into my LO Calc sheet.This is the error message I get:
<class 'ModuleNotFoundError'>: No module named 'gspread'
File "/usr/lib/libreoffice/program/pythonscript.py", line 1057, in getScript
mod = self.provCtx.getModuleByUrl( fileUri )
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/libreoffice/program/pythonscript.py", line 494, in getModuleByUrl
exec(code, entry.module.__dict__)
File "/home/yodap/.config/libreoffice/4/user/Scripts/python/PythonLibray/Weekly_update.py", line 1, in <module>
import gspread
File "/usr/lib/python3/dist-packages/uno.py", line 349, in _uno_import
return _builtin_import(name, *optargs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here is my Code:
import gspread
import uno
import json
from oauth2client.service_account import ServiceAccountCredentials
def fetch_data_from_google_sheets():
# Define the scope
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
# Load the credentials from the json key file
creds = ServiceAccountCredentials.from_json_keyfile_name("xxxxxxxxxx.json", scope)
# Authorize the clientsheet
client = gspread.authorize(creds)
# Get the instance of the Spreadsheet
spreadsheet = client.open("Portfolio")
# Get the worksheet by name
worksheet = spreadsheet.worksheet("Inputs")
# Get all values
values = worksheet.get_all_values()
return values
def import_into_calc(*_):
# Fetch data from Google Sheets
raw_data = fetch_data_from_google_sheets()
# Get the current document instead of loading a new one
desktop = XSCRIPTCONTEXT.getDesktop()
document = desktop.getCurrentComponent()
# Make sure we're operating on a spreadsheet
if not hasattr(document, "Sheets"):
raise Exception("This script must be run from a LibreOffice Calc document.")
inputsheet = document.Sheets['Inputsheet']
start_row_idx = 1 # Start writing from the second row
max_column_idx = 6 # Write up to column G (0-indexed)
# Write the data starting from the specified row, only up to the specified column
for row_idx, row_data in enumerate(raw_data):
for col_idx in range(max_column_idx + 1):
value = row_data[col_idx]
try:
# Convert to float if possible
inputsheet.getCellByPosition(col_idx, start_row_idx + row_idx).Value = float(value)
except ValueError:
# Leave as a string if not a number
inputsheet.getCellByPosition(col_idx, start_row_idx + row_idx).String = value.strip()
print("Data successfully imported into LibreOffice Calc.")
# Expose the main function for use within LibreOffice
g_exportedScripts = import_into_calc,
What am I doing wrong?