[Tutor] Remove a blank row of a Table

Alan Gauld alan.gauld at btinternet.com
Fri Feb 27 12:03:12 CET 2015


On 27/02/15 03:19, shouqiang1989 at sina.com wrote:
> Hello, everybody,      I came accross a question a few days
> ago. I wrote a Python script to copy something to a table in a word
> file. For some reaons, there are some blank rows. And I want to remove
> the blank rows. I tried many ways, but all didn't work. Below is the
> code. The code in blue is to remove the  blank row.

First thing is that there is no blue on a plain text email, so we can't 
see which lines. You need  to add a comment to alert us.

> import codecs
> import os
> import textwrap
> import win32com,string,shutil
> from win32com.client import Dispatch,constants

Most of your code seems to be based on calls to the win32com
library and the Word object model functions. Most of us on this
list don't work with that regularly (if at all) so you
probably will get a better response on the PyWin32 support
forum.

There is a Win32 mailing list too, on Gmane its

gmane.comp.python.windows


> def ReportPackage(reportName, reportApi, package=None):
>      desfile='C:\\Daten\\TestCASE5\\Model_hill.doc'
As a general style point its usually better to make filenames a 
parameter of the function. Otherwise the function is of limited
reuse potential as it can only ever work on one file.

Its not clear what reportApi is but you seem to call lots of functions 
on it. Is it a Win32 thing or another python module?

>      w = win32com.client.Dispatch('Word.Application')
>      w.Visible = 1
>      os.path.join(reportApi.GetReportDir(), reportName)

You call os.path.join here but do not store the result.
It does nothing.

>      fd = w.Documents.Open(desfile)              #open the model file    info = reportApi.GetInfo()

I assume the info assignment is supposed to be inline?

>      if package is None:
>          package = reportApi.GetMainPackage()
>          result = info.GetResult()
>          execTime = info.GetExecutionTime()
>          pkgName =package.GetName()
>      else:
>          result = package.GetResult()
>          execTime = package.GetTime()

No idea what that did but it seems irrelevant to most of what follows.

>      i=1
>      s=0
>      f=0
>      e=0

No idea what these are supposed to be for. Its much better
to use meaningful names. The cost of typing a few extra letters
is a small price to pay for the extra readability.

>
>      form = fd.Tables[6]

I'm guessing this is a hard coded dependency on the structure
of your document which we can't see. I'll just assume that
its correct.

>      form.Rows.Add()
>      form.Rows.Last.Cells[2].Range.InsertAfter("%(pkgName)s"  % {'pkgName': pkgName})
>      form.Rows.Last.Cells[3].Range.InsertAfter("%(executionTime)s" %{'executionTime': execTime})
>
>      form.Rows.Add()
>      lines = form.Rows.Last()
>      lines.Cells[0].Range.InsertAfter("Num")
>      lines.Cells[1].Range.InsertAfter("Time")
>      lines.Cells[2].Range.InsertAfter("Comment")
>      lines.Cells[3].Range.InsertAfter("Result")

This is all Word object model stuff so I'll just assume its correct.

>      testCase = package.GetTestCase(excludeSubPackages=reportApi.IsSet("hideSubPackages", "True"))

We don;t know what package does nor what reportApi does so
again I'll just assume you know what you are doing here.

And I don't know what testCase might be either, nor what the reportItem 
objects are. So I'll stop here.

Can you see the pattern? So much of what you are doing relies on 
external libraries that are not part of Python that it's almost 
impossible for us to comment on your code. You really need to find 
somebody familiar with the Word API rather than a Python expert.
You may be lucky and find one here, but a dedicated forum is
a much better bet.

We can comment on some finer points of your Python style but
that's probably not going to solve your real issue.

>          except :
>              break

But this one I can't let rest.

By doing this you are hiding all attempts by Python to alert you to 
problems. If anything goes wrong in the code above Python throws an 
exception which you just throw away. Python may be telling you the 
solution to your problem but you are ignoring it.

Always catch specific exceptions, but only if you know how to
handle them. Otherwise let Python help you by telling you what's
gone wrong.


> def _ReportProjectElement():
>      return()

The () after return is misleading, lose them.
It makes it look like you are calling a function
called return, which you aren't.

Its usually better when writing stubs to return a
hard coded example of valid output. If the function
doesn't return anything its more obvious if you
use pass rather than return. (IMHO at least :-)

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list