【发布时间】:2021-03-05 11:05:44
【问题描述】:
我有这个代码:
import random
from docx import Document
from docx.shared import Inches
title = "seans store"
"""
Declare the two dictionaries with name stock and prices
"""
stock = {
"banana": 60,
"apple": 150,
"orange": 120,
"pear": 150,
"tomatoes": 120,
"yoghurt": 100
}
prices = {
"yoghurt": 1,
"banana": 2,
"apple": 1,
"orange": .5,
"pear": 1.5,
"tomatoes": 1.1
}
"""
Declare the items within the customers shopping cart
"""
shopping_cart = [("pear",1), ("orange", 1), ("apple",2), ("tomatoes",2), ("yoghurt",0)]
for i in shopping_cart:
if stock[i[0]] - i[1] < 0:
shopping_cart.pop(shopping_cart.index(i))
else:
stock[i[0]] -= i[1]
print(f"Thank you for shopping @ {title.title()}")
for x in range(1):
Order_number = random.randint(100,999)
total_items = sum([i[1] for i in shopping_cart])
print(f"Your Order number is: {Order_number}")
print(f"Total items bought: {total_items}")
print("----------------")
print(" Bill")
print("----------------")
for i in shopping_cart:
print(f"{i[0]} x{i[1]} @ £{prices[i[0]]}")
total = (sum([prices[i[0]] * i[1] for i in shopping_cart]))
total_after_tax = total + (total*0.2)
tax_total = total_after_tax - total
print("----------------")
print("VAT @: £{:.2f}".format(tax_total))
print("----------------")
print("Total is £{:.2f}".format(total_after_tax))
"""Reciept creation"""
document = Document()
document.add_heading(f'Reciept for {title.title()}', 0)
p = document.add_paragraph('Items Ordered: ')
document.save("Reciept.docx")
我想从中创建一个文档,列出客户订购的商品及其价格作为收据。我希望创建它,以便将来可以自动化收据创建过程。我也很好奇能不能让代码完成后自动打印或者打开word文档软件。
【问题讨论】:
标签: python python-3.x list docx receipt