我正在寻找一种在报告输出中启用外来字符的方法。输出是HTML,我转换为PDF以使appengine发送PDF电子邮件。输出无法处理int。 chars例如。 AAO:
生成报告的代码是
class Report(webapp2.RequestHandler):
def get(self):
# Create a conversion request from HTML to PDF.
users = User.query()
today = date.today()
startdate = date(today.year, today.month, 1) # first day of month
html = None
for user in users:
if user.activity() > 0:
logging.info('found active user %s %s' % (user.firstname, user.lastname))
html = '<html><body><table border="1">'
level = user.level()
distributor = user
while distributor.has_downline():
downline = User.query(User.sponsor == distributor.key).order(User.lastname).fetch()
for person in downline: # to this for whole downline
orders = model.Order.all().filter('distributor_id =' , person.key.id()).filter('created >' , startdate).filter('status =', 'PAID').fetch(999999)
silver = 0
name = person.firstname +' '+ person.lastname
for order in orders:
logging.info('found orders')
for idx,item in enumerate(order.items):
purchase = model.Item.get_by_id(long(item.id()))
amount = int(order.amounts[idx])
silver = silver + amount*purchase.silver/1000.000
if len(name) > 13:
name = name[13]
html = html + '<tr><td>' + str(order.created.date().day)+'/'+ str(order.created.date().month )+'</td><td>' + filters.makeid(person.key.id()) +'</td><td>' + name + '</td><td>' + str(order.key().id()) + '</td><td>' + str(silver)
dist_level = order.dist_level
bonus = 0
if level == 5 and dist_level == 4:
bonus = 0.05
if level == 5 and dist_level == 3:
bonus = 0.1
if level == 5 and dist_level == 2:
bonus = 0.13
if level == 5 and dist_level == 1:
bonus = 0.35
if level == 4 and dist_level == 3:
bonus = 0.05
if level == 4 and dist_level == 2:
bonus = 0.08
if level == 4 and dist_level == 1:
bonus = 0.3
if level == 3 and dist_level == 2:
bonus = 0.03
if level == 3 and dist_level == 1:
bonus = 0.25
if level == 2 and dist_level == 1:
bonus = 0.2
html = html + '</td><td>' + str(bonus) + '</td><td>' + str(order.total)
bonusmoney = bonus * float(order.total)
html = html + '</td><td>' + str(bonusmoney) + '</td></tr>'
distributor = person
html = html + '</table>'
asset = conversion.Asset("text/html", html, "test.html")
conversion_obj = conversion.Conversion(asset, "application/pdf")
rpc = conversion.create_rpc()
conversion.make_convert_call(rpc, conversion_obj)
result = rpc.get_result()
if result.assets:
for asset in result.assets:
logging.info('emailing report')# to %s' % user.email)
message = mail.EmailMessage(sender='noreply@bnano.se',
subject='Report %s %s' % (user.firstname, user.lastname))
message.body = 'Here is the monthly report'
message.to = 'niklasro@gmail.com'
message.bcc = 'fridge@koolbusiness.com'
message.attachments = ['report.pdf', asset.data]
message.send()
logging.info('message sent')
在这种情况下应该采取什么措施来启用外国字符? 感谢
答案 0 :(得分:3)
Unicode编程的两个神圣规则:
您的代码似乎依赖于User.query解码输入(否则您的排序将无效!),但它对第二点没有任何作用。执行以下操作:
只需在输出中添加encoding = UTF8标记即可给出正确的最终结果,但如果您无法控制中间数据,则完全是偶然的。 (您还会注意到您的排序将会关闭。)
答案 1 :(得分:1)
当然,我只是猜测编码。
答案 2 :(得分:1)
您应该将<head>
添加到html = '<html><body><table border="1">'
,
这应该包含元标记:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
如果utf-8 charset无济于事,也许会有一些iso-8859-x。
答案 3 :(得分:1)
我的代码经历过类似的经历。我的母语是冰岛语,所以我们的数据中往往会有很多非英语字符。这个特殊的时间我已经在所有方面做了尽职调查,除了确保自己的.py文件被编码为utf-8而不是ansi,由于某些原因造成了不可预测的结果。
因此,如果所有其他方法都失败了,请检查您的文档编码并确保将其设置为utf-8。
答案 4 :(得分:0)
您可能需要查看htmllib.htmlentitydefs模块。并且不要忘记在输入表单和页面中声明编码,以确保所有数据均匀编码,或者至少始终了解它并采取相应的行动。