Home:ALL Converter>Ms Word and its fancy quotes breaks string compare

Ms Word and its fancy quotes breaks string compare

Ask Time:2014-08-14T08:33:01         Author:ashwinjv

Json Formatter

I am using win32com.client in python to open a word process, open a doc, and compare some text to the python variables that I have in my code.

For example:

I have in my code:

HeaderName = 'xyz'

I open the word doc and access the header text:

self.HeaderTable = self.doc.Sections(1).Headers(1).Range.Tables(1)
self.HeaderName = self.HeaderTable.Cell(1,1).Range.Text

and then I compare the two and print out pass or fail

if HeaderName == self.HeaderName:
    print('Pass')
else:
    print('Fail')

The issue is when the header contains a double quote. Ms Word uses fancy quotes and this breaks my string compare.

>>> chr(8220)
'“'
>>> chr(8221)
'”'
>>> chr(8220) == '"'
False
>>> chr(8221) == '"'
False

Any ideas on how I should proceed? I even tried replacing all the fancy quotes with regular quotes, but word just reverts back to the fancy ones. I dont want to do the following because I do a lot of string comparisons and dont want to go and change every individual one unless thats the only solution.

if headerName == self.HeaderName.replace(chr(8220), '"').replace(chr(8221),'"'):
    print('Pass')
else:
    print('Fail')

Author:ashwinjv,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25298326/ms-word-and-its-fancy-quotes-breaks-string-compare
yy