from openpyxl import loadworkbook wb =…
- User
from openpyxl import load_workbook wb = load_workbook(filename=r'C:\Users\LIQI\Documents\pythonfiles\xlwingsfiles\brinson.xlsm', read_only=True) sht = wb['Sheet1'] portfolioID = sht.range('b1').value#300Active,300Standard…… beginDate = sht.range('j1').value endDate = sht.range('l1').value analyst = sht.range('h1').value industry = sht.range('e1').value#一级行业 帮我看看错误怎么改 AttributeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_13508/579884863.py in ----> 1 portfolioID = sht.range('b1').value#300Active,300Standard…… 2 beginDate = sht.range('j1').value 3 endDate = sht.range('l1').value 4 analyst = sht.range('h1').value 5 industry = sht.range('e1').value#一级行业
AttributeError: 'ReadOnlyWorksheet' object has no attribute 'range'
- Assistant
The error message indicates that the 'ReadOnlyWorksheet' object does not have an attribute called 'range'.
To access the value of a specific cell in a spreadsheet using openpyxl's 'ReadOnlyWorksheet' object, you can use the 'cell' method. Here's an example of how you can modify your code to retrieve the values from cells 'B1', 'J1', 'L1', 'H1', and 'E1':
pythonportfolioID = sht.cell(row=1, column=2).value # Access cell B1 beginDate = sht.cell(row=1, column=10).value # Access cell J1 endDate = sht.cell(row=1, column=12).value # Access cell L1 analyst = sht.cell(row=1, column=8).value # Access cell H1 industry = sht.cell(row=1, column=5).value # Access cell E1Make sure to adjust the column number in the
sht.cell(row=X, column=Y)calls based on the column index of the cell you want to access.Note: The cell indices in openpyxl start from 1, so column 'B' corresponds to column index 2.
- User
if industry is not None: industry_data = portfolio_df[portfolio_df['Industry I'] == industry] plt.plot(industry_data['TRADEDATE'], industry_data['excess_return_compound']) plt.plot(industry_data['TRADEDATE'], industry_data['allocation_effect_compound']) plt.plot(industry_data['TRADEDATE'], industry_data['selection_effect_compound']) plt.plot(industry_data['TRADEDATE'], industry_data['interaction_effect_compound']) plt.xticks(rotation=45) plt.title(f"Excess Return in {industry}") plt.xlabel('TRADEDATE') plt.ylabel('excess_return_compound') plt.legend() 在jupyter运行这段代码 为什么不出图?
- Assistant
In Jupyter Notebook, you need to use the
plt.show()function to display the plot. Modify your code as follows:pythonif industry is not None: industry_data = portfolio_df[portfolio_df['Industry I'] == industry] plt.figure() plt.plot(industry_data['TRADEDATE'], industry_data['excess_return_compound']) plt.plot(industry_data['TRADEDATE'], industry_data['allocation_effect_compound']) plt.plot(industry_data['TRADEDATE'], industry_data['selection_effect_compound']) plt.plot(industry_data['TRADEDATE'], industry_data['interaction_effect_compound']) plt.xticks(rotation=45) plt.title(f"Excess Return in {industry}") plt.xlabel('TRADEDATE') plt.ylabel('excess_return_compound') plt.legend() plt.show()By adding
plt.figure()before plotting the data, you create a new figure for the plot. And is used to display the plot in Jupyter Notebook.