Python-docx: Prb with encoding of accents

Created on 25 May 2014  路  5Comments  路  Source: python-openxml/python-docx

Hi

I have used this beautiful piece of code and I am really astonished how easy it is to use.
Now, I have a problem. When generating a docx containing accents (french, spanish, german.... any!), wrong characters are printed. I am encoding in UTF8 before fetching the strings to add_run, but this does not work... Is this a bug?

Thanks for the answer

cheers

Most helpful comment

@kociak this is enough to reproduce it (after imports):

document = Document()
document.add_paragraph(unicode('toto茅', errors='replace'))
document.save('Debug.docx')

and this fixes it:

document = Document()
# document.add_paragraph(unicode('toto茅', errors='replace'))
document.add_paragraph(unicode('toto茅', 'utf-8'))
document.save('Debug.docx')

Unicode is a little tricky to get straight in one's head. I recommend some focused study there, working examples, until you know your way around it. This is a good place to start, although I recommend consulting multiple resources.
https://docs.python.org/2/howto/unicode.html

All 5 comments

I'll need something to go on. Can you post a (short) sample of code that exhibits the behavior and I'll see if I can reproduce? python-docx is designed and tested to work with unicode.

You are right

here the simplest example:

-_- coding: utf-8-_-

from docx import Document
from docx.shared import Inches

document = Document()
q=document.add_paragraph(style='CMDPause')

q.add_run(unicode('toto茅',errors='replace'))#title
document.save('Debug.docx')

Working on Mac OS 10.8.5
On my original file, where I parse a csv file, I don't need to add the first "coding" comment.
In both cases (this program or my actual program), i need the "unicode()" command otherwise I am getting an error stating that:
"ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters"

Any idea?

thanks for the help

@kociak this is enough to reproduce it (after imports):

document = Document()
document.add_paragraph(unicode('toto茅', errors='replace'))
document.save('Debug.docx')

and this fixes it:

document = Document()
# document.add_paragraph(unicode('toto茅', errors='replace'))
document.add_paragraph(unicode('toto茅', 'utf-8'))
document.save('Debug.docx')

Unicode is a little tricky to get straight in one's head. I recommend some focused study there, working examples, until you know your way around it. This is a good place to start, although I recommend consulting multiple resources.
https://docs.python.org/2/howto/unicode.html

dear Scanny

sounds like a newby error... thanks for the fix and sorry for not having found it on the web myself!
good luck in keeping developing your great piece of code!

Thank you @scanny very much. It helps me solve the same problem when working with japanese too!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kndidox picture kndidox  路  5Comments

scanny picture scanny  路  8Comments

madphysicist picture madphysicist  路  3Comments

norecces picture norecces  路  7Comments

aekoch picture aekoch  路  7Comments