[reportlab-users] Reportlab problem displaying japanese characters in a table

Michael Dorrian michael.dorrian at gmail.com
Mon Jun 15 11:16:25 EDT 2009


Hi Andy,Thanks to your help i got this working. Below is the code that
worked.Like you said using str() was a bad idea....but the main this is i
was able to dynamically add my rows using a loop and also correctly decode
the data to display correctly in the report.

def query(conn, sql_statement):
cur = conn.cursor()
cur.execute(sql_statement)
rows = cur.fetchall()
cur.close() #good idea with some DB APIs.
return rows

table_data = [[headers]]
JPYRows = query(conn, 'SELECT blah blah....')
for col in JPYRows:
firstCol = row[0].decode('shiftjis')
secondCol = row[1].decode('shiftjis')
......
TenthCol = row[9].decode('shiftjis')
table_data = [[firstCol,secondCol....TenthCol]]
t = Table(table_data)
story.append(t)

I appreciated the help alot..Thanks :)
Thanks,
Mike

On Fri, Jun 12, 2009 at 8:32 PM, Andy Robinson <andy at reportlab.com> wrote:


> 2009/6/12 Michael Dorrian <michael.dorrian at gmail.com>:

> > Thanks for the replys...........

> > Here is my function. I am just showing u what i had to do to display

> > the first field of the table correctly from the returned rows of the

> > sql.............there must be an easier way than this. The field

> > headings display correctly. Its just the database data thats the

> > problem.

>

> You said earlier that you get an error message when running this. Can

> you post the traceback? If it's in the table-building line, can you

> put this on a separate line....?

>

>

> str(..) requires its arguments to be ASCII, so whatever the content

> encoding is in JPYRows[0][11], if it contains any non-ascii, it will

> fail. Try getting the first field directly and converting it from

> utf8 to unicode on a separate line and then assembling it. We'll then

> know where it fails. e.g.

>

> db_content = JPYRows[0][11]

> unicode_content = db_content.decode('utf8')

> t = Table(Paragraphs(...headers..., (unicode_content, ....)))

>

>

> ...if that fails, try the other Japanese encodings in plave of 'utf8'

> - do you know anything about EUC or Shift-JIS?

>

>

>

>

>

> BTW, all those steps you are going through are necessary. If you are

> doing them in several parts of your program, you can hide them in a

> function somewhere to make it more readable; every project I work on

> tends to have a database connection already passed around, and I have

> a utility function something like this...

>

> def query(conn, sql_statement):

> cur = conn.cursor()

> cur.execute(sql_statement)

> rows = cur.fetchall()

> cur.close() #good idea with some DB APIs.

> return rows

>

> ...so the actual code I'd work on from project to project looks more

> like...

>

>

> table_data = [[headers]] + query(conn, 'SELECT blah blah....')

> t = Table(table_data)

> story.append(t)

>

>

> --

> Andy Robinson

> CEO/Chief Architect

> ReportLab Europe Ltd.

> Media House, 3 Palmerston Road, Wimbledon, London SW19 1PG, UK

> Tel +44-20-8545-1570

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users

>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20090616/785a67d5/attachment.html>


More information about the reportlab-users mailing list