希望从 Python 中的代码创建一个 word 文档
Looking to create a word document from code in Python
我有这个代码:
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文档软件
如果我没理解错的话——你打算将打印语句输出到word文档中。唯一的方法是通过 docx
库添加 .add_paragraph()
方法。
的一个很好的例子
from docx import Document
from docx.shared import Inches
document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')
document.add_paragraph(
'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
'first item in ordered list', style='List Number'
)
document.add_picture('monty-truth.png', width=Inches(1.25))
document.add_page_break()
document.save('demo.docx')
保存文件后,只需使用 os
模块以标准方式打开文件即可将其打开。这将默认为您设置为打开 .docx
类型文件的任何应用程序。
import os
os.startfile('C:\Path\To\demo.docx')
我有这个代码:
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文档软件
如果我没理解错的话——你打算将打印语句输出到word文档中。唯一的方法是通过 docx
库添加 .add_paragraph()
方法。
from docx import Document
from docx.shared import Inches
document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')
document.add_paragraph(
'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
'first item in ordered list', style='List Number'
)
document.add_picture('monty-truth.png', width=Inches(1.25))
document.add_page_break()
document.save('demo.docx')
保存文件后,只需使用 os
模块以标准方式打开文件即可将其打开。这将默认为您设置为打开 .docx
类型文件的任何应用程序。
import os
os.startfile('C:\Path\To\demo.docx')