[python-win32] Excel Characters object

Niemann, Hartmut hartmut.niemann at siemens.com
Tue May 27 14:27:45 CEST 2014


Hello!

I want to create an Excel file through the python COM interface.
I can open the file, fill the cells, save the file.

Now I want to color single characters in the cell content.

In VBA ( see
http://stackoverflow.com/questions/7618121/excel-vba-change-color-of-certain-characters-in-a-cell)
I would do
Cell.Characters(i, 1).Font.Color = vbRed

How does that translate to Python?
mycell.Characters[x:y] does not work, nor anything else I tried.

mycell.Characters is a "win32com.gen_py.Microsoft Excel 12.0 Object Library.Characters instance",
but what can I do with it?

In the gen_py generated interface, the Characters class is defined as:
----8<--------------------
class Characters(DispatchBaseClass):
                CLSID = IID('{00020878-0000-0000-C000-000000000046}')
                coclass_clsid = None

                def Delete(self):
                               return self._ApplyTypes_(117, 1, (12, 0), (), u'Delete', None,)

                def Insert(self, String=defaultNamedNotOptArg):
                               return self._ApplyTypes_(252, 1, (12, 0), ((8, 1),), u'Insert', None,String
                                               )

                _prop_map_get_ = {
                               # Method 'Application' returns object of type 'Application'
                               "Application": (148, 2, (13, 0), (), "Application", '{00024500-0000-0000-C000-000000000046}'),
                               "Caption": (139, 2, (8, 0), (), "Caption", None),
                               "Count": (118, 2, (3, 0), (), "Count", None),
                               "Creator": (149, 2, (3, 0), (), "Creator", None),
                               # Method 'Font' returns object of type 'Font'
                               "Font": (146, 2, (9, 0), (), "Font", '{0002084D-0000-0000-C000-000000000046}'),
                               "Parent": (150, 2, (9, 0), (), "Parent", None),
                               "PhoneticCharacters": (1522, 2, (8, 0), (), "PhoneticCharacters", None),
                               "Text": (138, 2, (8, 0), (), "Text", None),
                }
                _prop_map_put_ = {
                               "Caption": ((139, LCID, 4, 0),()),
                               "PhoneticCharacters": ((1522, LCID, 4, 0),()),
                               "Text": ((138, LCID, 4, 0),()),
                }
                def __iter__(self):
                               "Return a Python iterator for this object"
                               try:
                                               ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
                               except pythoncom.error:
                                               raise TypeError("This object does not support enumeration")
                               return win32com.client.util.Iterator(ob, None)
                #This class has Count() property - allow len(ob) to provide this
                def __len__(self):
                               return self._ApplyTypes_(*(118, 2, (3, 0), (), "Count", None))
                #This class has a __len__ - this is needed so 'if object:' always returns TRUE.
                def __nonzero__(self):
                               return True
----8<--------------------


Mit freundlichen Grüßen
Dr. Hartmut Niemann

Siemens AG
Infrastructure & Cities Sector
Rail Systems Division
Locomotives and Components
IC RL LOC EN CCI 1
Werner-von-Siemens-Str. 67
91052 Erlangen, Deutschland
Tel: +49 9131 7-34264
Fax: +49 9131 7-26254
mailto:hartmut.niemann at siemens.com

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20140527/8a6c014d/attachment.html>


More information about the python-win32 mailing list