0

I am trying to paste a covariance matrix on an existing xlsm file through the below formula, but it is not working. The Cov Matrix is an array. Could you please help me with it?

Error I get is: [Errno 13] Permission denied: 'C:\\Users\\gm\\AppData\\Local\\Continuum\\anaconda3\\Files for Python\\Book2.xlsm'
shrunk_covariance = array([[0.44067332, 0.16161585], [0.16161585, 0.80226294]])

Code to create the covariance matrix (you might not need to look at it, but here it is for sake of completeness):

import numpy as np
from sklearn.covariance import LedoitWolf
real_cov = np.array([[.4, .2],[.2, .8]])


np.random.seed(0)
X = np.random.multivariate_normal(mean=[0, 0],cov=real_cov,size=50)

cov = LedoitWolf().fit(X)
shrunk_covariance = cov.covariance_
shrunk_covariance

Code that does not work:

import openpyxl 

from openpyxl import load_workbook

wb = load_workbook(r"C:\Users\gm\AppData\Local\Continuum\anaconda3\Files for Python\Book2.xlsm", keep_vba = True)

# grab the active worksheet
ws = wb['Shrunk_CovLast']

# Data can be assigned directly to cells
ws.cell(row=1,column=1).value = print(shrunk_covariance)

# Save the file
wb.save(r"C:\Users\gm\AppData\Local\Continuum\anaconda3\Files for Python\Book2.xlsm")
Ahmad
  • 1,618
  • 5
  • 24
  • 46
GM13
  • 19
  • 3
  • 1
    you have to close the files opened in other programs before you can write to them – luigigi Nov 19 '19 at 09:40
  • 1
    This error usually pops up, when you have a previously generated file open and python cannot write to that file, because of that. – AnsFourtyTwo Nov 19 '19 at 09:41
  • Good points. I closed the file and the code ran smoothly and saved the file. Problem is that when i open the file, nothing is in there. When i change the code to write "1" into the first cell, it works, while if i keep "print(shrunk_covariance)" nothing is written in the file. – GM13 Nov 19 '19 at 09:48
  • Possible duplicate of [PermissionError \[errno 13\] when running openpyxl python script in Komodo](https://stackoverflow.com/questions/31147460/permissionerror-errno-13-when-running-openpyxl-python-script-in-komodo) – Charlie Clark Nov 19 '19 at 10:09
  • the issue was that the file is open, not that the path is not specified (as in the other question suggested by charlie Clark). Now the problem is that the array i am trying to write in the excel file does not get written – GM13 Nov 19 '19 at 10:21
  • Remove the print from print(shrunk_covariance) – ConSod Nov 25 '19 at 20:54

1 Answers1

0

You might not have write permission for the file Book2.xlsm; You could save to another filename, or modify the file permissions. To find out about the permissions of the file you are trying to save, try ls -l filename (where filename is the name of your file) in a unix system, and you can change the permissions using chmod u+w filename . On windows I blv. you need to login as admin and right click on the file to modify the permissions; to see permissions you don't need to be admin.

jeremy_rutman
  • 3,552
  • 4
  • 28
  • 47