0

enter image description hereI am trying to write list of strings in pdf file using below script.

pdf = fpdf.FPDF(format='letter')
pdf.add_page()
pdf.set_font("Arial", size=12)

for i in data:
    pdf.write(5,str(i))
    pdf.ln()
pdf.output("testings.pdf")

But, I am getting error

UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' in position 1548: ordinal not in range(256)

data contain results in string format Can anyone please help me to know how to write results in pdf file, I want to write each string in data list separate page.

Above is data screenshot The length is huge so not inserting all records but list contain text data only

user3734568
  • 1,311
  • 2
  • 22
  • 36
  • Is data a list of numbers? Then you should first convert it to string. – toti08 Aug 16 '18 at 12:53
  • Possible duplicate of [UnicodeEncodeError: 'latin-1' codec can't encode character](https://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character) – LeandroN. Aug 16 '18 at 13:12
  • @toti08, Data a list text , there are total strings which I am trying to write in PDF file – user3734568 Aug 16 '18 at 14:21
  • Could you post your `data` variable? – toti08 Aug 16 '18 at 14:27
  • @toti08, I have inserted screenshot of data which i am trying to write in PDF – user3734568 Aug 16 '18 at 14:44
  • 1
    Thanks! I think your problem lays in the double quote character, which has code `201c`, you should better use utf-8 coding. – toti08 Aug 16 '18 at 14:48
  • @toti08, It worked thank you so much. I changed below line in fpdf.py file."p = self.pages[n].encode("utf-8") if PY3K else self.pages[n]" But now I want each string in separate page in PDF file – user3734568 Aug 16 '18 at 14:51

1 Answers1

0

This is because MySQLdb normally tries to encode everythin to latin-1. This can be fixed by executing the following commands right after you've etablished the connection: I also have the same problem Python MySQLdb module. Since MySQL will let you store just about any binary data you want in a text field regardless of character set, I found my solution here Link

user2965711
  • 77
  • 1
  • 9