⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 133.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 2 页
字号:
>>> win32com.server.register.UnregisterClasses(TaxApplication)

					</pRE>

					<P>A very comprehensive example of using Microsoft Word and Excel is stored in the <Tt claSS="monofont">testMSOffice.py</TT> file, which is part of your PythonWin distribution. It's worth checking out!!!<a namE="idx1073744475"></A><A Name="idx1073744476"></a><A NAMe="idx1073744477"></a><a name="idx1073744478"></a><a name="idx1073744479"></a><a name="idx1073744480"></a><A naMe="idx1073744481"></a><a Name="idx1073744482"></a><A namE="idx1073744483"></A><A Name="idx1073744484"></a><A NAMe="idx1073744485"></a><a nAME="idx1073744486"></A><a namE="idx1073744487"></A><A Name="idx1073744488"></a></p>

					<h5>Word</h5>
						<p>The following code implements a <a name="idx1073744489"></a><a name="idx1073744490"></a>simple wrapper for the Microsoft Word Application. To <A naMe="idx1073744491"></a>test it you need to create a Word document and replace its path in the code. The program will open this file, replace the first occurrence of the string <tT clasS="monofont">"#name#"</tt> within the file, add a small bit of text to the end of the line, and print the file.</p>

						<PRE>
							
import win32com.client
False = 0
True = -1
wdLine = 5

class WordApp:
    def __init__(self):
        self.app = win32com.client.Dispatch("Word.Application")
    def open(self, document_file):
        self.app.Documents.Open(document_file)
    def replace(self, source_selection, new_text):
        self.app.Selection.HomeKey(Unit=wdLine)
        self.app.Selection.Find.Text = source_selection
        self.app.Selection.Find.Execute()
        self.app.Selection.TypeText(Text=new_text)
    def addtext(self, new_text):
        self.app.Selection.EndKey(Unit=wdLine)
        self.app.Selection.TypeText(Text=new_text)
    def printdoc(self):
        self.app.Application.PrintOut()
    def close(self):
        self.app.ActiveDocument.Close(SaveChanges =False)

worddoc = WordApp()
worddoc.open(r"s:\ template.doc")
worddoc.replace("#name#", "Andre Lessa")
worddoc.addtext(" What do you want to learn ?")
worddoc.printdoc()
worddoc.close

						</Pre>

						<p>If you type in the name of the object's attribute that accesses the <a NAME="idx1073744492"></a><a naME="idx1073744493"></A><Tt claSS="monofont">Dispatch</TT> method, you get as a result, the COM object name:</p>

						<pre>
							
&gt;&gt;&gt; worddoc.app
&lt;<i>COMObject Word.Application.</i>&gt;

						</pre>

						<p>This object is an example of a <a name="idx1073744494"></a><a namE="idx1073744495"></a><i>dynamic dispatch</I> object. The provided name indicates that the object is a generic COM object, and affirms that Python doesn't know anything about it, except the name that you used to create it. All the information about this object is built dynamically.</p>

						<p>Besides dynamic dispatches, you can also use <a Name="idx1073744496"></a><A namE="idx1073744497"></A><I>static dispatches,</I> which involve the generation of a <tt clASS="monofont">.py</Tt> file that contains support for the specific COM object. In CORBA speak, this is called stub generation, or IDL compilation.</p>

						<p>In order to generate the Python files that support a specific COM object, you need to execute <tT CLAss="monofont">win32com\client\makepy.py.</tt> A list of Type Libraries will be displayed. Select one (for example, 'Microsoft Word 8.0 Object Library') and click OK. You can also call the makepy.py program directly from the command prompt by typing <B><TT Class="monofont">makepy.py "Microsoft Word 8.0 Object Library".</tt></b><a name="idx1073744498"></a><a name="idx1073744499"></a><a NamE="idx1073744500"></a><a nAme="idx1073744501"></a><a Name="idx1073744502"></A><A NAme="idx1073744503"></a><a NAME="idx1073744504"></a><a naME="idx1073744505"></A><A name="idx1073744506"></A><A NAme="idx1073744507"></a><a name="idx1073744508"></a><a name="idx1073744509"></a><a name="idx1073744510"></A><a nAme="idx1073744511"></a><A name="idx1073744512"></A><a naME="idx1073744513"></A></P>

						<p>Now, Python knows exactly how to handle the interfaces before invoking the COM object. Although, you can't see any differences, you can check that Python really knows something else now by querying the COM object:</p>

						<prE>
							
&gt;&gt;&gt; import win32com.client
&gt;&gt;&gt; wd=win32com.client.Dispatch("Word.Application")
&gt;&gt;&gt; wd
&lt;win32com.gen_py.Microsoft Word 8.0 Object Library._Application&gt;

						</PRE>

						<p>Note that Python knows the explicit type of the object now.</p>

						<p>All the compiled information is stored in a file in the <tT CLAss="monofont">win32com/gen_py</tt> directory. You probably won't understand the filename because it is encoded. Actually, you don't need to use this file at all. All the interface information is made available via <TT CLass="monofont">win32com.client.Dispatch</tt> and <tt class="monofont">win32com.client.constants.</tt></p>

						<p>If you really need to <a namE="idx1073744514"></a><a Name="idx1073744515"></A><a namE="idx1073744516"></a><a nAME="idx1073744517"></A><a namE="idx1073744518"></A>identify the name of the module that was generated, you can use the <A Name="idx1073744519"></a><A NAMe="idx1073744520"></a><tt CLASs="monofont">win32com.client.gencache</tt> module. This module has two functions: <tt class="monofont">GetModuleForCLSID</tt> and <tt class="monofont">GetModuleForProgID</tT> that return Python module objects you can use in your code.</p>

						<p><A namE="idx1073744521"></a><a naMe="idx1073744522"></a><tT CLAss="monofont">makepy.py</tt> also automatically installs all generated constants from a library of types in an object called <TT CLass="monofont">win32com.clients.constants.</tT> After creating the object, all the constants become available to you.</P>

						<P>In the previous example, we had to initialize the constant <Tt claSS="monofont">wdLine,</TT> because the constants were not available. Now, after running <tt class="monofont">makepy.py,</tt> you can replace the line</p>

						<pre>
							
self.app.Selection.EndKey(Unit=wdLine)

						</pre>

						<p>with</p>

						<prE>
							
self.app.Selection.EndKey(Unit=win32com.clients.constants.wdLine)

						</prE>

						<p>and remove the initialization line</p>

						<pRe>
							
wdLine = 5

						</pre>

						<P>The next example uses the <tt cLASS="monofont">wdWindowStateMaximize</tt> constant to maximize Microsoft Word:</p>

						<pRE>
							
&gt;&gt;&gt; w.WindowState = win32com.client.constants.wdWindowStateMaximize

						</PRe>

					
					<h5>Excel</h5>
						<p>Next, we'll see how to <A NAMe="idx1073744523"></a><a nAME="idx1073744524"></A>create COM clients using Microsoft Excel. The principle is very simple. Actually, it is the same one used previously for wrapping Microsoft Word, as it is demonstrated in the following example.</p>

						<pre>
							
&gt;&gt;&gt; import win32com.client
&gt;&gt;&gt; excelapp = win32com.client.Dispatch("Excel.Application")
&gt;&gt;&gt; excelapp.Visible = 1

						</pre>

						<p>Note that we have to <a name="idx1073744525"></a><a name="idx1073744526"></a><A naMe="idx1073744527"></a><a Name="idx1073744528"></a><A namE="idx1073744529"></A>change the <TT clasS="monofont">Visible</TT> property in order to see the Excel application. The default behavior is to hide the application window because it saves processor cycles. However, the object is available to any COM client that asks for it.</P>

						<p>As you can see in the example, Excel's progid is <tt cLASS="monofont">Excel.Application.</tt></p>

						<p>After you create the Excel object, you are able to call its methods and set its properties. Keep in mind that the Excel Object Model has the following hierarchy: Application, WorkBook, Sheet, Range, and Cell.</P>

						<P>Let's play a little with Excel. The following statements write to the workbook:</P>

						<Pre>
							
&gt;&gt;&gt; excelapp.Range("A1:C1").Value = "Hello", "Python", "World"
&gt;&gt;&gt; excelapp.Range("A2:A2").Value = 'SPAM! SPAM! SPAM!'

						</pre>

						<p>Note that you can also use <a name="idx1073744530"></a><a name="idx1073744531"></a><a nAme="idx1073744532"></A>tuples to transport values:</p>

						<prE>
							
&gt;&gt;&gt; excelapp.Range("A1:C1").Value = ('Hello', 'Python', 'World')

						</pre>

						<p>To print a selected area, you need to use the <A namE="idx1073744533"></A><A Name="idx1073744534"></a><TT CLass="monofont">PrintOut()</tT> method:</P>

						<PRe>
							
&gt;&gt;&gt; excelapp.Range("A1:C1").PrintOut()

						</pre>

						<P>What about entering date and time information? The following examples will show you how to <A NAme="idx1073744535"></a><a name="idx1073744536"></a><a name="idx1073744537"></a>set the Date/Time format for Excel cells.<a name="idx1073744538"></A><a nAme="idx1073744539"></a><A name="idx1073744540"></A><a naME="idx1073744541"></A><A name="idx1073744542"></A><A NAme="idx1073744543"></a><a NAME="idx1073744544"></a><a naME="idx1073744545"></A><A name="idx1073744546"></a><a name="idx1073744547"></a><a name="idx1073744548"></a><a naMe="idx1073744549"></a><A namE="idx1073744550"></a><a naMe="idx1073744551"></a><a NAME="idx1073744552"></a><a naME="idx1073744553"></A></P>

						<p>First, call Excel's time function:</p>

						<prE>
							
&gt;&gt;&gt; excelapp.Cells(4,3).Value = "=Now()"
&gt;&gt;&gt; excelapp.Columns("C").EntireColumn.AutoFit()

						</PRE>

						<p>The <a naME="idx1073744554"></A><A name="idx1073744555"></a><tt class="monofont">AutoFit()</tt> function is required in order to display the information, instead of showing <tt clasS="monofont">"#######".</tt></P>

						<p>Now, use Python to set the time you want:</p>

						<pRe>
							
&gt;&gt;&gt; import time, pythoncom
&gt;&gt;&gt; excelapp.Cells(4,1).Value = pythoncom.MakeTime(time.time())
&gt;&gt;&gt; excelapp.Range("A4:A4").NumberFormat = "d/mm/yy h:mm"
&gt;&gt;&gt; excelapp.Columns("A:C").EntireColumn.AutoFit()

						</pre>

						<P>Note that the <a naME="idx1073744556"></A><A name="idx1073744557"></A><TT Class="monofont">Cells()</TT> structure works like a numeric array. That means that instead of using Excel's notation of letters and numbers, you need to think of the spreadsheet as a numeric matrix.<A Name="idx1073744558"></a><A NAMe="idx1073744559"></a><a name="idx1073744560"></a><a name="idx1073744561"></a><a name="idx1073744562"></a><A naMe="idx1073744563"></a><a Name="idx1073744564"></a><A namE="idx1073744565"></A><A Name="idx1073744566"></a><A NAMe="idx1073744567"></a><a nAME="idx1073744568"></A><a namE="idx1073744569"></A><A Name="idx1073744570"></a><a name="idx1073744571"></a><a name="idx1073744572"></a></p>

					
				
				<h4>Visual Basic</h4>
					<p>In order to implement a COM object using Python you need to implement a Python class that exposes the functionality to be exported. It is also necessary to assign two special attributes to this class, as required by the Python COM implementation.</P>

					<p>The first attribute is the Class ID (<tT claSs="monofont">_reg_clsid_</tt>). This attribute must contain a UUID, which can be generated by calling the <a Name="idx1073744573"></A><A NAme="idx1073744574"></a><tT CLAss="monofont">pythoncom.CreateGuid()</tt> function. The other attribute is a friendly string that you will use to call the COM object (<TT CLass="monofont">_reg_progid_</tT>), as follows:</P>

					<PRe>
						
class COMCalcServer:
    _reg_clsid_ = '{ C76BEA61-3B39-11D4-8A7C-444553546170} '
    _reg_progid_ = 'COMCALCSERVER.VERSION1'
    _public_methods_ = ['mul','div','add','sub']
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -