<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.E-MailFormatvorlage17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="DE" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hello!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-US">I want to create an Excel file through the python COM interface.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I can open the file, fill the cells, save the file.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Now I want to color single characters in the cell content.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">In VBA ( see<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><a href="http://stackoverflow.com/questions/7618121/excel-vba-change-color-of-certain-characters-in-a-cell">http://stackoverflow.com/questions/7618121/excel-vba-change-color-of-certain-characters-in-a-cell</a>)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I would do<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Cell.Characters(i, 1).Font.Color = vbRed<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">How does that translate to Python?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">mycell.Characters[x:y] does not work, nor anything else I tried.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">mycell.Characters is a “win32com.gen_py.Microsoft Excel 12.0 Object Library.Characters instance”,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">but what can I do with it?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">In the gen_py generated interface, the Characters class is defined as:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">----8<--------------------<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">class Characters(DispatchBaseClass):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> CLSID = IID('{00020878-0000-0000-C000-000000000046}')<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> coclass_clsid = None<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> def Delete(self):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return self._ApplyTypes_(117, 1, (12, 0), (), u'Delete', None,)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> def Insert(self, String=defaultNamedNotOptArg):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return self._ApplyTypes_(252, 1, (12, 0), ((8, 1),), u'Insert', None,String<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> )<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> _prop_map_get_ = {<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> # Method 'Application' returns object of type 'Application'<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> </span><span lang="FR">"Application": (148, 2, (13, 0), (), "Application", '{00024500-0000-0000-C000-000000000046}'),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="FR"> </span><span lang="EN-US">"Caption": (139, 2, (8, 0), (), "Caption", None),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "Count": (118, 2, (3, 0), (), "Count", None),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "Creator": (149, 2, (3, 0), (), "Creator", None),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> # Method 'Font' returns object of type 'Font'<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> </span><span lang="FR">"Font": (146, 2, (9, 0), (), "Font", '{0002084D-0000-0000-C000-000000000046}'),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="FR"> </span><span lang="EN-US">"Parent": (150, 2, (9, 0), (), "Parent", None),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "PhoneticCharacters": (1522, 2, (8, 0), (), "PhoneticCharacters", None),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "Text": (138, 2, (8, 0), (), "Text", None),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> _prop_map_put_ = {<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "Caption": ((139, LCID, 4, 0),()),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "PhoneticCharacters": ((1522, LCID, 4, 0),()),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "Text": ((138, LCID, 4, 0),()),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> def __iter__(self):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> "Return a Python iterator for this object"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> try:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> except pythoncom.error:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> raise TypeError("This object does not support enumeration")<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return win32com.client.util.Iterator(ob, None)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> #This class has Count() property - allow len(ob) to provide this<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> def __len__(self):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return self._ApplyTypes_(*(118, 2, (3, 0), (), "Count", None))<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> #This class has a __len__ - this is needed so 'if object:' always returns TRUE.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> def __nonzero__(self):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return True<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">----8<--------------------<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Arial","sans-serif"">Mit freundlichen Grüßen<br>
Dr. Hartmut Niemann<br>
<br>
Siemens AG<br>
Infrastructure & Cities Sector<br>
Rail Systems Division<br>
Locomotives and Components<br>
IC RL LOC EN CCI 1<br>
Werner-von-Siemens-Str. 67<br>
91052 Erlangen, Deutschland<br>
Tel: +49 9131 7-34264<br>
Fax: +49 9131 7-26254<br>
<a href="mailto:hartmut.niemann@siemens.com"><span style="color:blue">mailto:hartmut.niemann@siemens.com</span></a><br>
<br>
</span><span style="font-size:8.0pt;font-family:"Arial","sans-serif"">Siemens Aktiengesellschaft: Vorsitzender des Aufsichtsrats: Gerhard Cromme; Vorstand: Joe Kaeser, Vorsitzender; Roland Busch, Klaus Helmrich, Hermann Requardt, Siegfried Russwurm, Ralf P.
Thomas; Sitz der Gesellschaft: Berlin und München, Deutschland; Registergericht: Berlin Charlottenburg, HRB 12300, München, HRB 6684; WEEE-Reg.-Nr. DE 23691322
</span><o:p></o:p></p>
</div>
</body>
</html>