How to call VBA WorksheetFunction.Match using xlwings worksheet.api? - excel

I have an Excel table from which I need to look up a specific value in column A and want to get the row number. For example, column A in the Excel sheet contains numbers from 1 to 50 and cell B2=10.
I have tried to call WorksheetFunction.Match(arg1,arg2,arg3) (https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.worksheetfunction.match?view=excel-pia#Microsoft_Office_Interop_Excel_WorksheetFunction_Match_System_Object_System_Object_System_Object_)
using xlwings worksheet.api, but get and "AttributeError: .WorksheetFunction" when using it in Python (the WorksheetFunction.Match() works fine in VBA).
import xlwings as xw
wb=xw.Book(r'test.xlsm')
ws=wb.sheets['Test']
row_number=ws.api.WorksheetFunction.Match(Range("B2").Value, Range("A1:A50"), 0)
In this example I expect to get row_number=10, but instead get:
Traceback (most recent call last):
File "C:\tools\anaconda3\5.3.0\envs\nilspy\lib\site-packages\xlwings\_xlwindows.py", line 117, in __getattr__
v = getattr(self._inner, item)
File "C:\tools\anaconda3\5.3.0\envs\nilspy\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.WorksheetFunction
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\tools\anaconda3\5.3.0\envs\nilspy\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-24-2c337b4c3c07>", line 5, in <module>
ws.api.WorksheetFunction.Match(Range("B2").Value, Range("A1:A50"), 0)
File "C:\tools\anaconda3\5.3.0\envs\nilspy\lib\site-packages\xlwings\_xlwindows.py", line 137, in __getattr__
self._oleobj_.GetIDsOfNames(0, item)
pywintypes.com_error: (-2147352570, 'Unknown name.', None, None)
I'm grateful for all help!
Edit: The last row in the code should probably refer to ws.range, like this:
import xlwings as xw
wb=xw.Book(r'test.xlsm')
ws=wb.sheets['Test']
ws.api.WorksheetFunction.Match(ws.range('B2').value, ws.range('A1:A50'), 0)
However, it results in the same error.

You could use find ( I can't find any documentation on worksheet functions being called)
ws = wb.sheets['Test']
search_range = ws.range("A1:A" + str(ws.range("A" + str(ws.cells.rows.count)).end('up').row))
search_range.api.find(ws.range("B2").value).row
The value may not be present so you would be better setting result of find to a variable first and testing if None:
found = search_range.api.find(ws.range("B2").value)
r = found.row if found is not None else '-999' #some value to indicate not found

Related

Openpyxl recognize data: TypeError: 'method' object is not subscriptable and is not a valid coordinate or range error

I'm new to openpyxl. I need to copy several columns from a file and paste it on another file, with the same columns.
I'm starting my code, but getting an error:
file1 = load_workbook('PRODUCTION.xlsx') ws = file1.active column = ws.cell['ID'] print (column)
I get this error:
Traceback (most recent call last): File "c:\Users\Ana\Documents\PRODUCTION Project\production.py", line 29, in <module> column = ws.cell['ID'] TypeError: 'method' object is not subscriptable
And when I tried only column = ws ['ID']
I get:
Traceback (most recent call last): File "c:\Users\Ana\Documents\PRODUCTION Project\production.py", line 29, in <module> column = ws ['ID'] File "C:\Users\Ana\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\worksheet\worksheet.py", line 290, in __getitem__ min_col, min_row, max_col, max_row = range_boundaries(key) File "C:\Users\Ana\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\utils\cell.py", line 135, in range_boundaries raise ValueError(msg) ValueError: ID is not a valid coordinate or range PS C:\Users\Ana\Documents\PRODUCTION Project>
Thanks in advance.

Python3 Pandas.DataFrame.info() Error Key: 30

So I was digging around some datasets, and trying to use pandas to analyze then and i stumbled across the following error.. and my brain froze :(
here is the snippet where the exception is being raised
import pandas as pd
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
data = pd.DataFrame(X)
data['class'] = y
data.head()
data.tail()
data.columns
print('length of data is', len(data))
data.shape
data.info()
here's the error trackback
C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\Scripts\python.exe C:/Users/97150/PycharmProjects/EmbeddedLinux/AI/project.py
length of data is 569
Traceback (most recent call last):
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\indexes\base.py", line 2889, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 97, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 30
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/97150/PycharmProjects/EmbeddedLinux/AI/project.py", line 42, in <module>
data.info()
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\frame.py", line 2587, in info
self, verbose, buf, max_cols, memory_usage, null_counts
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\io\formats\info.py", line 250, in info
self._verbose_repr(lines, ids, dtypes, show_counts)
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\io\formats\info.py", line 335, in _verbose_repr
dtype = dtypes[i]
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
return self._get_value(key)
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\series.py", line 991, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\indexes\base.py", line 2891, in get_loc
raise KeyError(key) from err
KeyError: 30
Process finished with exit code 1
note: I'm using PyCharm community 2020.2, and checked for updates and such, and nothing changed
So, turned out, pandas is straight up acting weird.
removing the () from the data.info() fixed the issue :)
You can alternatively try passing verbose=True and null_counts=True arguments to the .info() method to display the result(you can just use verbose argument if you don't want to consider null values).
data.info(verbose=True, null_counts=True)
Let me know if things work out for you.

I have a problem with writing to my excel file

I am getting error message when im trying to write to my excel file.
On this line: df_Percent_Change.to_excel(writer, sheet_name=x, startcol=8)
Traceback (most recent call last):
File "/Users/david.soderstrom/Dropbox (Diagona)/DS/Python/Byggmarknad_index/Byggmarknad_index.py", line 125, in
df_Percent_Change.to_excel(writer, sheet_name=x, startcol=8)
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 1766, in to_excel
engine=engine)
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/pandas/io/formats/excel.py", line 652, in write
freeze_panes=freeze_panes)
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/pandas/io/excel.py", line 1742, in write_cells
wks = self.book.add_worksheet(sheet_name)
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py", line 179, in add_worksheet
return self._add_sheet(name, worksheet_class=worksheet_class)
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py", line 666, in _add_sheet
name = self._check_sheetname(name, isinstance(worksheet, Chartsheet))
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py", line 717, in _check_sheetname
if len(sheetname) > 31:
TypeError: object of type 'int' has no len()
Exception ignored in: >
Traceback (most recent call last):
File "/Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py", line 154, in del
Exception: Exception caught in workbook destructor. Explicit close() may be required for workbook.
# Calculate and save the percent change for each asset
if 'Percent_Change' not in excel_db.columns:
print('Percent_Change does not exist in excel file.')
print('Calculating...')
# Loop through and read each sheet
x = 0
for x in range(countSheets):
# Read in data for the calculation
data = pd.read_excel('databas.xlsx', sheet_name=x, index_col='Date')
# Calculate the percent change from day to day
Percent_Change = data['Adj Close'].pct_change()*100
print(type(Percent_Change))
df_Percent_Change = pd.DataFrame(Percent_Change)
print(type(df_Percent_Change))
writer = pd.ExcelWriter('databas.xlsx', engine='xlsxwriter')
df_Percent_Change.to_excel(writer, sheet_name=x, startcol=8)
# Save the result
writer.save()
writer.close()
x += 1

openpyxl 'TypeError: expected <class 'int'>'

Python 3.6 Newest openpyxl
So, I'm working with an excel using openpyxl. I have a very boring test.xlsx excel, which I can read with the following code:
import os
import openpyxl
wb = openpyxl.load_workbook('test.xlsx')
sheet = wb['Sheet1']
print(sheet)
However, when I change to a fairly complex excel document 'apqptest.xlsx.'
import os
import openpyxl
wb = openpyxl.load_workbook('apqptest.xlsx')
sheet = wb['Sheet1']
print(sheet)
I get the following error (I have created the same sheet1 on the apqptest.xlsx document just fyi):
C:\Users\nkent\PycharmProjects\webscraper\venv\Scripts\python.exe "C:/Users/nkent/Desktop/Programs/Quote Preperation.py"
Traceback (most recent call last):
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\base.py", line 57, in _convert
value = expected_type(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/nkent/Desktop/Programs/Quote Preperation.py", line 3, in <module>
wb = openpyxl.load_workbook('apqptest.xlsx')
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\reader\excel.py", line 274, in load_workbook
for c in find_charts(archive, rel.target):
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\chart\reader.py", line 43, in find_charts
drawing = SpreadsheetDrawing.from_tree(tree)
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 100, in from_tree
return cls(**attrib)
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\drawing\graphic.py", line 53, in __init__
self.rot = rot
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\base.py", line 69, in __set__
value = _convert(self.expected_type, value)
File "C:\Users\nkent\PycharmProjects\webscraper\venv\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'int'>

How to iterate over column names with PyTable?

I have a large matrix (15000 rows x 2500 columns) stored using PyTables and getting see how to iterate over the columns of a row. In the documentation I only see how to access each row by name manually.
I have columns like:
ID
X20160730_Day10_123a_2
X20160730_Day10_123b_1
X20160730_Day10_123b_2
The ID column value is a string like '10692.RFX7' but all other cell values are floats. This selection works and I can iterate the rows of results but I cannot see how to iterate over the columns and check their values:
from tables import *
import numpy
def main():
h5file = open_file('carlo_seth.h5', mode='r', title='Three-file test')
table = h5file.root.expression.readout
condition = '(ID == b"10692.RFX7")'
for row in table.where(condition):
print(row['ID'].decode())
for col in row.fetch_all_fields():
print("{0}\t{1}".format(col, row[col]))
h5file.close()
if __name__ == '__main__':
main()
If I just iterate with "for col in row" nothing happens. As the code is above, I get a stack:
10692.RFX7
Traceback (most recent call last):
File "tables/tableextension.pyx", line 1497, in tables.tableextension.Row.__getitem__ (tables/tableextension.c:17226)
KeyError: b'10692.RFX7'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tables/tableextension.pyx", line 126, in tables.tableextension.get_nested_field_cache (tables/tableextension.c:2532)
KeyError: b'10692.RFX7'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./read_carlo_pytable.py", line 31, in <module>
main()
File "./read_carlo_pytable.py", line 25, in main
print("{0}\t{1}".format(col, row[col]))
File "tables/tableextension.pyx", line 1501, in tables.tableextension.Row.__getitem__ (tables/tableextension.c:17286)
File "tables/tableextension.pyx", line 133, in tables.tableextension.get_nested_field_cache (tables/tableextension.c:2651)
File "tables/utilsextension.pyx", line 927, in tables.utilsextension.get_nested_field (tables/utilsextension.c:8707)
AttributeError: 'numpy.bytes_' object has no attribute 'encode'
Closing remaining open files:carlo_seth.h5...done
You can access a column value by name in each row:
for row in table:
print(row["10692.RFX7"])
Iterate over all columns:
names = table.coldescrs.keys()
for row in table:
for name in names:
print(name, row[name])

Resources