Python 应该如何启用外来字符?
我正在寻找一种在我的报告输出中启用外来字符的方法。输出是HTML,我将其转换为PDF,以使appengine发送PDF电子邮件。输出无法处理整数字符,例如: 生成报告的代码是Python 应该如何启用外来字符?,python,google-app-engine,google-cloud-datastore,python-2.7,Python,Google App Engine,Google Cloud Datastore,Python 2.7,我正在寻找一种在我的报告输出中启用外来字符的方法。输出是HTML,我将其转换为PDF,以使appengine发送PDF电子邮件。输出无法处理整数字符,例如: 生成报告的代码是 class Report(webapp2.RequestHandler): def get(self): # Create a conversion request from HTML to PDF. users = User.query() today = da
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')
类报告(webapp2.RequestHandler):
def get(自我):
#创建从HTML到PDF的转换请求。
users=User.query()
今天=日期。今天()
startdate=日期(今天.年,今天.月,1)#月的第一天
html=无
对于用户中的用户:
如果user.activity()大于0:
logging.info('找到活动用户%s%s'(user.firstname,user.lastname))
html=“”
level=user.level()
分销商=用户
而distributor.has_downline():
downline=User.query(User.shandor==distributor.key).order(User.lastname.fetch())
对于下线人员:#对于整个下线
orders=model.Order.all().filter('distributor_id=',person.key.id()).filter('created>',startdate.).filter('status=','PAID').fetch(99999)
银=0
name=person.firstname+“”+person.lastname
对于订单中的订单:
logging.info('找到订单')
对于idx,枚举中的项(order.items):
purchase=model.Item.get\u by\u id(long(Item.id()))
金额=整数(订单金额[idx])
银=银+金额*purchase.silver/1000.000
如果len(名称)>13:
名称=名称[13]
html=html++'+str(order.created.date().day)+'/'+str(order.created.date().month)+'+filters.makeid(person.key.id())++'+name++'+str(order.key().id())+'+str(silver)
距离级别=订单距离级别
奖金=0
如果级别==5,距离级别==4:
奖金=0.05
如果级别==5且距离级别==3:
奖金=0.1
如果级别==5且距离级别==2:
奖金=0.13
如果级别==5且距离级别==1:
奖金=0.35
如果级别==4,距离级别==3:
奖金=0.05
如果级别==4,距离级别==2:
奖金=0.08
如果级别==4,距离级别==1:
奖金=0.3
如果级别==3且距离级别==2:
奖金=0.03
如果级别==3且距离级别==1:
奖金=0.25
如果级别==2且距离级别==1:
奖金=0.2
html=html+''+str(奖金)+''+str(订单总数)
bonusmoney=奖金*浮动(订单总数)
html=html++'+str(bonusmoney)+''
分销商=人
html=html+“”
asset=conversion.asset(“text/html”,html,“test.html”)
转换=转换。转换(资产,“应用程序/pdf”)
rpc=conversion.create_rpc()
转换。进行转换调用(rpc、转换对象)
result=rpc.get_result()
如果是结果。资产:
对于result.assets中的资产:
logging.info(“电子邮件报告”)#发送到%s“%user.email”)
message=mail.EmailMessage(发件人=)noreply@bnano.se',
subject='Report%s%s'(user.firstname,user.lastname))
message.body='这是月报'
message.to=niklasro@gmail.com'
message.bcc=fridge@koolbusiness.com'
message.attachments=['report.pdf',asset.data]
message.send()
logging.info('消息已发送')
在这种情况下,应该如何启用外来字符?
谢谢它看起来好像在处理它们,但编码是错误的。您可能需要执行诸如name.decode('utf-8')、encode('latin-1')之类的操作
当然,我只是在猜测编码。它看起来好像在处理它们,但编码是错误的。您可能需要执行诸如name.decode('utf-8')、encode('latin-1')之类的操作 当然,我只是猜测编码。您应该将
添加到html='
,
这应该包含元标记:
如果utf-8字符集不起作用,可能一些iso-8859-x会起作用。您应该将
添加到html='
,
这应该包含元标记:
如果utf-8字符集不起作用,可能一些iso-8859-x会起作用。Unicode编程的两条神圣规则:
- 确定输入的编码是什么,并对其进行适当的解码
- 明确输出的编码方式
- 确保person.firstname字段确实是Unicode字符串。如果需要检查内容,请查看每个字符的十六进制表示形式;只需打印字符串就会导致一些自动编码并隐藏字符串