0

I am simply trying to write my captured variable contents into my new PDF cell. If I take out the variables, and write static content such as txt="Hey Dave!" this creates and writes to new pdf just fine. However, I am struggling with figuring out why this is difficult. Below is full code, followed by console error when trying to use variables.

from fpdf import FPDF
import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(r"C:\coolPath\language\python\Msg-2-PDF\test_msg.msg")

print (msg.SenderName)
print (msg.SenderEmailAddress)
print (msg.SentOn)
print (msg.To)
print (msg.CC)
print (msg.BCC)
print (msg.Subject)
print (msg.Body)

SenderName = msg.SenderName
SenderEmailAddress = msg.SenderEmailAddress
SentOn = msg.SentOn
To = msg.To
CC = msg.CC
BCC = msg.BCC
Subject = msg.Subject
Body = msg.Body

count_attachments = msg.Attachments.Count
if count_attachments > 0:
    for item in range(count_attachments):
        print (msg.Attachments.Item)(item + 1).Filename

del outlook, msg

pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Hey Dave!" + Body, ln=1, align="C") # fails on 'Body', when removed writes 'Hey Dave' to PDF as supposed to.
pdf.output("simple_demo.pdf")

Error/Trace Back when variable is used:

Traceback (most recent call last):
  File "script.py", line 42, in <module>
    pdf.output("simple_demo.pdf")
  File "C:\Users\developer\AppData\Local\Continuum\anaconda3\lib\site-packages\fpdf\fpdf.py", line 1065, in output
    self.close()
  File "C:\Users\developer\AppData\Local\Continuum\anaconda3\lib\site-packages\fpdf\fpdf.py", line 246, in close
    self._enddoc()
  File "C:\Users\developer\AppData\Local\Continuum\anaconda3\lib\site-packages\fpdf\fpdf.py", line 1636, in _enddoc
    self._putpages()
  File "C:\Users\developer\AppData\Local\Continuum\anaconda3\lib\site-packages\fpdf\fpdf.py", line 1170, in _putpages
    p = self.pages[n].encode("latin1") if PY3K else self.pages[n]
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2013' in position 275: ordinal not in range(256)
Dr Upvote
  • 8,023
  • 24
  • 91
  • 204
  • AFAIK the Arial font set does not support the use of unicode. [This answer](https://stackoverflow.com/a/25930929/5260982) references a font that sounds like it would help and [this answer](https://stackoverflow.com/a/21530540/5260982) points to an alternative package to do it. There is also [TfPDF](http://www.fpdf.org/en/script/script92.php) which may help. – Dave Jun 25 '19 at 15:00
  • I think OP is looking for an answer related to the Python version of FPDF, not the PHP version. – Erik Kalkoken Jul 04 '19 at 22:11
  • Possible duplicate of [UnicodeEncodeError: 'latin-1' codec can't encode character '\u2013' (writing to PDF)](https://stackoverflow.com/questions/56761449/unicodeencodeerror-latin-1-codec-cant-encode-character-u2013-writing-to) – Erik Kalkoken Jul 04 '19 at 22:11

0 Answers0