기본 콘텐츠로 건너뛰기

Problem with COM and MsExcel (Brunain Christophe)

■ 파이썬_02
[python-win32] Re: Problem with COM and MsExcel (Brunain Christophe)
Niels Steen Krogh nielssteenkrogh@hotmail.com
Wed, 02 Apr 2003 19:45:31 +0200

Previous message: [python-win32] Problem with COM and MsExcel
Next message: [python-win32] "Casting" COM objects
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

--------------------------------------------------------------------------------

Hi!
This script might help you - use CoInitialize ..... It helped me in a
situation similar to yours.

Good luck
Niels


from win32com.client import Dispatch
   from pythoncom import CoInitialize,CoUninitialize
   CoInitialize() #start com thread - kan udelades, men vil mindske 
stabiliteten
   o=Dispatch("Excel.Application") #start excel
   o.Visible=0 #excel k?rer i baggrunden
   o.Workbooks.Open(Filename='c:\\dokumenter\\mappe14.xls') #get the 
workbook
   o.Workbooks(1).Sheets(1).Cells.Clear() #clear the workbook
   sht=o.Workbooks(1).Sheets(1) #peg p? ark 1
   sht.Range(sht.Cells(1,1),sht.Cells(len(data),len(data[0]))).Value=data 
#put a range of data (tupple formatted)
   o.Workbooks(1).Save() #gem regneark
   o.Workbooks(1).Close(SaveChanges='0') #close workbook without dialog
   o.Quit() #luk excel
   del o #slet objekt
   del sht #slet objekt
   del data #slet objekt
   CoUninitialize() #close com thread



Cand. Polit.
Niels Steen Krogh
Solsortvej 44
2000 F.

Tlf: 3888 8613

ZiteLab / Empower your data





>From: python-win32-request@python.org
>Reply-To: python-win32@python.org
>To: python-win32@python.org
>Subject: Python-win32 digest, Vol 1 #325 - 3 msgs
>Date: Wed, 02 Apr 2003 12:00:14 -0500
>
>Send Python-win32 mailing list submissions to
> python-win32@python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/python-win32
>or, via email, send a message with subject or body 'help' to
> python-win32-request@python.org
>
>You can reach the person managing the list at
> python-win32-admin@python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Python-win32 digest..."
>
>
>Today's Topics:
>
> 1. Re: Using py2exe with COM server question (Wolf Logan)
> 2. Re: Using py2exe with COM server question (Cory Dodt)
> 3. Problem with COM and MsExcel (Brunain Christophe)
>
>--__--__--
>
>Message: 1
>From: "Wolf Logan"
>To:
>Subject: Re: [python-win32] Using py2exe with COM server question
>Date: Tue, 1 Apr 2003 11:30:38 -0800
>
>CreateGuid() will create (in most cases) a CLSID that is unique. that means
>that no one else will ever produce that same CLSID. in that case, you can
>distribute your package with that CLSID hard-coded, and it'll work just
>fine -- it willnever collide with another CLSID.
>
>non-python objects that are distributed in other ways also use a hard-coded
>CLSID. the trick is to create a *new* CLSID when your object is revised
>sufficiently that its interface changes.
>
>----- Original Message -----
>From: "Larry Bates"
>Sent: Tuesday, April 01, 2003 7:28 AM
>
>
> > Problem:
> >
> > I'm confused about how to 'properly' handle the _reg_clsid_
> > attribute of the class. It is my understanding that this needs
> > to come from pythoncom.CreateGuid() on the machine where the COM
> > object is being installed and it is unique to that machine.
> > If this is the case, what is the best way to make this happen in
> > a 'frozen' COM object? The example shown has a hard coded GUID
> > and that just doesn't seem right for distribution to other machines.
>
>
>
>--__--__--
>
>Message: 2
>Date: Tue, 1 Apr 2003 13:15:07 -0800 (PST)
>From: Cory Dodt
>To: python-win32@python.org
>Subject: [python-win32] Re: Using py2exe with COM server question
>
> > I'm confused about how to 'properly' handle the _reg_clsid_
> > attribute of the class. It is my understanding that this needs
> > to come from pythoncom.CreateGuid() on the machine where the COM
> > object is being installed and it is unique to that machine.
>
>Guid stands for 'globally unique ID', with emphasis on the word global.
>The recipe shows the guid hard-coded because that really is the right way
>to do it. A Guid is a promise that any object with this clsid conforms to
>the same interface as every other object with that guid, on any machine,
>anywhere in the universe.
>
>It's perfectly ok to 'print pythoncom.CreateGuid()' in an interactive
>browser and paste that into your _reg_clsid_.
>
>
>__________________________________________________
>Do you Yahoo!?
>Yahoo! Tax Center - File online, calculators, forms, and more
>http://platinum.yahoo.com
>
>
>--__--__--
>
>Message: 3
>From: Brunain Christophe
>To: "Pythonwin (E-mail)"
>Date: Wed, 2 Apr 2003 08:46:03 +0200
>Subject: [python-win32] Problem with COM and MsExcel
>
>Hello all !
>
>I begin tu use python win and the COM features
>
>I try to do a little scripyt that takes a text file (an unload from a
>database) and to put it in an excel worksheet
>
>It works good, but only one time
>
>the second time I execute the script, i only se the menubar and the =
>toolbar
>on the top of the screen, the document itself is not displaying
>
>I see only what was my screen before the execution of the script
>
>If i close all the excel sheets and look in the task manager, I see an
>EXCEL.EXE still running
>
>If i make an "end process" it works, but if i try to run the script =
>once
>again, it makes an error.
>
>here is the script i use
>
>import win32com.client
>
>ex =3D win32com.client.Dispatch("Excel.Application")
>ex.Visible =3D 1
>ex.Workbooks.Open("C:\\test\\test.xls")
>ex.Run("SaveText","C:\\test\\codes", "C:\\test\\codes2.xls")
>ex.Quit
>
>and the traceback i got when i close the excel.exe and run the script =
>once
>again
>
>PythonWin 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on =
>win32.
>Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - =
>see
>'Help/About PythonWin' for further copyright information.
>Traceback (most recent call last):
> File
>"C:\Python22\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py"=
>,
>line 307, in RunScript
> debugger.run(codeObject, __main__.__dict__, start_stepping=3D0)
> File =
>"C:\Python22\lib\site-packages\Pythonwin\pywin\debugger\__init__.py",
>line 60, in run
> _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
> File =
>"C:\Python22\lib\site-packages\Pythonwin\pywin\debugger\debugger.py",
>line 591, in run
> exec cmd in globals, locals
> File "C:\test\test1.py", line 4, in ?
> ex.Visible =3D 1
> File "C:\Python22\lib\site-packages\win32com\client\dynamic.py", line =
>504,
>in __setattr__
> raise AttributeError, "Property '%s.%s' can not be set." %
>(self._username_, attr)
>AttributeError: Property 'Excel.Application.Visible' can not be set.
>
>Does someone know what's wrong ?
>i've looked in the archives of this mailing list but i haven't found :(
>
>Thanks a lot for your answers
>
>PS :
>Sorry if my english is not so good
>
>Brunain Christophe
>Service informatique de l'expertise m=E9dicale
>SPF Sant=E9 Publique, S=E9curit=E9 de la chaine alimentaire et =
>Environnement
>
>
>
>--__--__--
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32@python.org
>http://mail.python.org/mailman/listinfo/python-win32
>
>
>End of Python-win32 Digest


_________________________________________________________________
Send s?de postkort til s?de mennesker http://www.msn.dk/postkort





--------------------------------------------------------------------------------


Previous message: [python-win32] Problem with COM and MsExcel
Next message: [python-win32] "Casting" COM objects
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

댓글

이 블로그의 인기 게시물

80040154 오류로 인해 CLSID가 {xxxx-...}인 구성 요소의 COM 클래스 팩터리를 검색하지 못했습니다.

원문보기 .NET 으로 만든 응용프로그램에서 com 객체를 호출한 경우 Windows7 64bit 에서 제목과 같은 에러가 발생했다. Win32 COM 과 .NET 프로그램간의 호환성 때문에 생긴 문제였다. 원인은 .NET 실행시 JIT 컴파일러에 의해 최적화된 기계어로 변환되기 때문.. Win32 COM은 컴파일시.. Win32 COM에 맞춰 빌드 속성에서 하위버전으로 맞춰 컴파일을 다시하는 방법도 있지만 메인 프로젝트가 .NET이라면 참조되는 모든 프로젝트를 다 바꿔야할 노릇.. 또 다른 방법은 COM+를 이용하여 독립적으로 만드는 것이다. 분리시키는 방법은 아래 주소해서 확인할 수 있다. http://support.microsoft.com/kb/281335 나의 경우는 Win32 COM DLL을 64비트 .NET 프로그램에서 참조하니 COM 객체를 제대로 호출하지 못하였습니다. 그래서 .NET 프로그램의 Target Machine을 x86으로 설정하니 제대로 COM 객체를 호출하였습니다.

[Pyinstaller] 실행 파일 관리자 권한 획득하기

고객사에서 일부 사용자에게서 프로그램 오류가 발생한다며 아래와 같이 에러 캡처를 보내왔습니다. 프로그램에서 로그를 남기기 위해 로그 파일을 생성하는데 권한의 문제로 로그 파일을 생성하지 못해 프로그램 오류가 발생한 것 같습니다. 처음에는 Python 코드에서 관리자 권한을 요청하는 코드를 넣으려고 했는데, 실제로 Stackoverflow를 찾아보면 이런 내용이 나옵니다. 프로그램이 관리자 권한으로 실행되지 않았다면 관리자 권한으로 다시 프로그램을 실행시키는 코드입니다. import os import sys import win32com.shell.shell as shell ASADMIN = 'asadmin' if sys.argv[-1] != ASADMIN: script = os.path.abspath(sys.argv[0]) params = ' '.join([script] + sys.argv[1:] + [ASADMIN]) shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params) sys.exit(0) 하지만 개인적으로 이런 방식은 마음에 들지 않았고 조금 더 찾아보니 Pyinstaller로 exe 파일을 만들 때 옵션을 설정하여 관리자 권한을 요청하도록 할 수 있다고 합니다. --uac-admin을 옵션에 추가하면 프로그램 실행 시 관리자 권한을 요청할 수 있습니다. pyinstaller.exe --uac-admin sample.py 하지만 안타깝게도 이 방식은 원하는 대로 동작하지 않았습니다. 마지막으로 manifest 파일을 이용하여 시도해보았습니다. spec 파일을 이용하여 pyinstaller로 빌드하면 <실행 파일 이름>.manifest 라는 파일이 생성됩니다. 파일에서 아랫부분을 찾아볼 수 있습니다. <security> <re

초간단 프로그램 락 걸기

프로그램에 락을 걸 일이 생겨났다. 하드웨어 락을 걸면 쉬울텐데 그 정도는 아니고 프로그램의 실행 날짜를 제한 해 달라고 한다. 그래서 파일(license.lic)을 가지고 락을 걸리고 결정을 했다. 요구 사항은 아래와 같다. 1. license.lic 파일이 없으면 프로그램을 실행 할수 없게 한다. 2. 지정한 날짜를 넘어서는 프로그램을 실행 할수 없게 한다. 3. 사용자가 시스템 날짜를 되돌렸을때 인식하여 프로그램을 실행 할수 없게 한다. 음.... 1.번 문제는 사용자가 프로그램을 실행하기 위해서 license.lic 파일을 받아야만 한다. license.lic 파일에는 최근 실행 날짜/종료날짜 이런식으로 적도록 한다.(물론 내용은 암호화 한다.) 최근 실행날짜는 프로그램이 실행때마다 업데이트 하도록 하고 시스템 날짜와 비교하여 시스템 날짜가 최근 실행 날짜보다 이전의 날짜면 시스템 날짜를 되돌렸다고 인식하도록 한다.(3.번 문제 해결) 시스템 날짜와 종료 날짜를 비교하여 시스템 날짜가 종료 날짜를 넘으면 프로그램을 실행 할수 없도록 한다.(2.번 문제 해결)