问题描述
如何在此代码中在表格上方添加图片
def sales_transaction(self,request,pk):
response = HttpResponse(content_type='application/pdf')
download_name = "SalesTransactions.pdf"
response['Content-Disposition'] = 'attachment; filename={}'.format(download_name)
header = ["Transaction","Date","Amount","Buying Amount","Status","Payment mode"]
# End writing
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table
from reportlab.platypus import Image,Paragraph,TableStyle
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4,landscape
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter
from io import BytesIO
from reportlab.lib import colors
from PIL import Image
cm =2.54
elements = []
a = Image.open("/home/dalalns/Documents/transaction_details.png")
a.drawHeight = 2 * inch
a.drawWidth = 2 * inch
data = [['1',a],['3','4']]
c = canvas.Canvas(download_name,pagesize=landscape(A4))
doc = SimpleDocTemplate(response,rightMargin=0,leftMargin=1 * cm,topMargin=0.3 * cm,bottomMargin=0)
user = request.user
phone_number=user
queryset = DirectTransactions.objects.filter(cashbook__user=user).order_by('-created_at')
queryset = DirectTransactionsViewSet(request=request).filter_queryset(queryset=queryset)
data = [["Date","Description","Sales","Spending","Profit"],]
total_sale=0
total_spending=0
total_profit=0
for ct in queryset:
if ct.buying_amount:
buying_amount=ct.buying_amount
else:
buying_amount=0
total_sale=total_sale+ct.amount
total_spending =total_spending+buying_amount
total_profit = total_profit+ct.amount-buying_amount
row = [
ct.txn_date,ct.txn_note,round(ct.amount,2),round(buying_amount,round((ct.amount - buying_amount),2)
]
data.append(row)
data.append(["Total","",round(total_sale,round(total_spending,round(total_profit,2)])
table = Table(data,colWidths=80,rowHeights=30)
table.setStyle(TableStyle([
('INNERGRID',(0,0),(-1,-1),0.25,colors.black),('BOX',('BACKGROUND',colors.lightgrey)
]))
table.wrapOn(c,200,400)
table.drawOn(c,20,50)
c.save()
elements.append(table)
doc.build(elements)
return response
如何设置表格样式以使其看起来更优雅,以及如何在表格上方添加图像,如果我想在表格上方添加文字,该怎么做。 我尝试使用此代码,但无法正常工作,图像似乎未进入pdf。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)