3

I am facing an issue with setting a value of Excel Cell. I get data from a table cell in MS-Word Document(dcx) and print it on output console.

Problem is that the data of the cell is just a word, "Hour", with no apparent other leading or trailing printable character like white-spaces. But when I print it using python's print() function, it shows some unexpected character, more like a small "?" in a rectangle.

I don't know where does it come from.

And when I write the same variable that holds the word, "Hour", to an Excel cell it shows a bold dot(.) in the cell.

What can be the problem?

Any help is much appreciated.

I Am Using Python 3.2 And PyWin32 3.2 On Win7. Thanks.

Aashiq Hussain
  • 543
  • 2
  • 8
  • 17

2 Answers2

9

I have the same problem when I get data from a table in word document. What I did to solve this problem is to write a small function that removes all these unnecessary characters:

import re

def removechars(cellvalue):
    text = re.sub(r"[\r\n\t\x07\x0b]", "", cellvalue)
    return text

Then I use :

value = table.Cell(Row = 1, Column = 1).Range.Text
value = removechars(value)
YusuMishi
  • 2,317
  • 1
  • 18
  • 8
3

Try using value.rstrip('\r\n') to remove any carriage returns (\r) or newlines (\n) at the end of your string value.

bossylobster
  • 9,993
  • 1
  • 42
  • 61
  • `\x07` is equivalent to `\a` which is a system beep. I'm not sure how that made it into your string. – bossylobster May 03 '12 at 01:55
  • Is there any way to strip all those special characters like, \t, \a, \n and others.? I mean without using all of them in the strip function? – Aashiq Hussain May 03 '12 at 02:12
  • No. The important thing here, and in the future when you are programming, is understanding why they look that way. Why don't you post the code you are using to retrieve the value. – bossylobster May 03 '12 at 03:18