Reputation: 583
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.
Upvotes: 3
Views: 4603
Reputation: 2397
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)
Upvotes: 9
Reputation: 10163
Try using value.rstrip('\r\n')
to remove any carriage returns (\r
) or newlines (\n
) at the end of your string value
.
Upvotes: 3