我只是想将捕获的变量内容写到新的PDF单元格中。如果我取出变量,然后编写静态内容(例如txt="Hey Dave!"
),则可以创建并写入新的pdf。但是,我正在努力弄清为什么这很困难。以下是完整的代码,并在尝试使用变量时出现控制台错误。
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")
使用变量时出现错误/跟踪:
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)