Can I Pass the path name as Funtions parameter for open the file

89 views
Skip to first unread message

rahul

unread,
Apr 7, 2011, 9:06:29 AM4/7/11
to python-excel, rahul....@lntinfotech.com
Hi,
I have one module file with following Code:

def append (): ----------------------line 1
exl = win32.gencache.EnsureDispatch('Excel.Application')
wb1 = exl.Workbooks.Open(r'D:\my1\Con_report.xlsx')
wb2 = exl.Workbooks.Open(r'D:\my1\report1)---------------------
line4
ws1 = wb1.Worksheets("Sheet1")
ws2 = wb2.Worksheets("Sheet1")
list1 = []
list2 = []
for i in range(1,40):
for j in range(1,5):
#value = ws1.Cells(i,j).Value
list1.append(ws1.Cells(i,j).Value)
if list2 == list1 :
if list1 == [None, None, None, None]:
rows = i
for x in range(1,40):
for y in range(1,5):
print rows+x-1,y
ws1.Cells(rows+x-1,y).Value =
ws2.Cells(x,y).Value
break
list2 = list1
del list1[:]

Can i use def append(a) where a is the path name of the Excel file.And
that excel file i want to open (in line 4)Is that possible

John Machin

unread,
Apr 7, 2011, 5:06:06 PM4/7/11
to python...@googlegroups.com, rahul....@lntinfotech.com

On Thursday, April 7, 2011 11:06:29 PM UTC+10, rahul wrote:
Hi,
 I have one module file with following Code:

def append ():    ----------------------line 1
    exl = win32.gencache.EnsureDispatch('Excel.Application')
    wb1 = exl.Workbooks.Open(r'D:\my1\Con_report.xlsx')
    wb2 = exl.Workbooks.Open(r'D:\my1\report1)---------------------
line4
    ws1 = wb1.Worksheets("Sheet1")
    ws2 = wb2.Worksheets("Sheet1")
[snip]

Can i use def append(a) where a is the path name of the Excel file.And
that excel file i want to open (in line 4)Is that possible

Yes, but use a more meaningful name than "a".  Have you tried it? What happened? 

Michael

unread,
Apr 7, 2011, 5:39:51 PM4/7/11
to python-excel
To answer your question yes you can pass a path string to your append
function.

def append(workbook):
wb1 = exl.Workbooks.Open(workbook)

append(r'path_to_file')

it appears that you are pretty new to python so it would might be
helpful for you to read over the python tutorial http://docs.python.org/tutorial/index.html
section 4.6 talks about defining functions in python

Just a heads up I do not think your code is going to do what you want
it to. While I have not used win32com (for my purposes everything I
need to do can be handled by xlrd and xlwt - see tutorial on
http://www.python-excel.org/ ) I am guessing that ws1.Cells(i,j).Value
will either return None if the cell is empty or the cell value. You
are only appending to list1 so list2 == list1 will never be true

rahul

unread,
Apr 8, 2011, 3:58:32 AM4/8/11
to python-excel
Yes I have tried in Python IDE by writing a following statement:
import excel -----it is a module in which append function defined
path = “D\my1\report1.xlsx”
excel.append(path)
after that I got error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
excel.append(path)
File "C:\Python27\Lib\excel.py", line 22, in append
ws1 = wb1.Worksheets("Sheet1")
File "C:\Python27\lib\site-packages\win32com\client\__init__.py",
line 502, in __getattr__
if d is not None: return getattr(d, attr)
File "C:\Python27\lib\site-packages\win32com\client\__init__.py",
line 463, in __getattr__
return self._ApplyTypes_(*args)
File "C:\Python27\lib\site-packages\win32com\client\__init__.py",
line 456, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes,
*args),
com_error: (-2147417848, 'The object invoked has disconnected from its
clients.', None, None)

John Machin

unread,
Apr 8, 2011, 6:13:27 PM4/8/11
to python...@googlegroups.com
On Friday, April 8, 2011 5:58:32 PM UTC+10, rahul wrote:
Yes I have tried in Python IDE by writing a following statement:
import excel -----it is a module in which append function defined
path  =  “D\my1\report1.xlsx”

That is NOT what you ran; those squiggly MS-Word-style quotes would cause a syntax error. Don't type from memory; use copy/paste.

Even with the syntax error fixed, it won't run for TWO reasons: (1) you need to use a raw string so that \r will be interpreted by the Python compiler as two bytes, not a single  (carriage return) byte (2) you need a : after the D

   r"D:\my1\report1.xlsx"
 
excel.append(path)
after that I got error:
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    excel.append(path)
  File "C:\Python27\Lib\excel.py", line 22, in append

You shouldn't install your own modules there. That should be reserved for the installed Python files and nothing else. You run the risk of replacing a Python library file with one of your own. Put them in c:\python\lib\site-packages instead (AFTER you've debugged them).
 
    ws1 = wb1.Worksheets("Sheet1")
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py",
line 502, in __getattr__
    if d is not None: return getattr(d, attr)
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py",
line 463, in __getattr__
    return self._ApplyTypes_(*args)
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py",
line 456, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes,
*args),
com_error: (-2147417848, 'The object invoked has disconnected from its
clients.', None, None)

This looks like Excel has stopped running. Googling the error message  indicates a threading problem (???).

I suggest that you step through your excel.py when it is running to try to find out what it is doing. If that doesn't help, we'd need to see the current version of excel.py. Put it in dropbox or similar to avoid cluttering up this forum.
Reply all
Reply to author
Forward
0 new messages