项目依赖1:python-docx
项目依赖2:jinja
pip install docxtpl
模板:
效果:
from docx import Document
from docxtpl import DocxTemplate
tpl = DocxTemplate('差旅报销单.docx')
context = {
'year': '2020',
'month': '08',
'day': '08',
'depart': '销售部',
'username': '宋公明',
}
tpl.render(context)
tpl.save('差旅报销单123123.docx')
from docx import Document
from docxtpl import DocxTemplate
tpl = DocxTemplate('差旅报销单.docx')
context = {
'date': '2020年08月08日',
'depart': '销售部',
'username': '宋公明',
'fee_list': [
{
"uid": "2a2f409d-3c20-44ec-b2ae-8364da6b8477",
"startDate": "2020-08-04",
"startPoint": "1",
"arriveDate": "2020-08-04",
"arrivePoint": "2",
"transport": "高铁",
"transportFee": "0.00",
"days": 0.0,
"missionAllowance": "0.00",
"hotelExpense": "0.00",
"urbanTransport": "0.00",
"otherExpense": "0.00",
},
{
"uid": "3f26ac5b-3f10-4917-971a-4609137a196e",
"startDate": "2020-08-01",
"startPoint": "常州",
"arriveDate": "2020-08-03",
"arrivePoint": "济南",
"transport": "高铁",
"transportFee": "300.00",
"days": 0.0,
"missionAllowance": "0.00",
"hotelExpense": "0.00",
"urbanTransport": "0.00",
"otherExpense": "0.00",
},
{
"uid": "910bb069-88a2-480c-a653-ae3bb8a7c84f",
"startDate": "2020-08-01",
"startPoint": "杭州",
"arriveDate": "2020-08-01",
"arrivePoint": "杭州",
"transport": "高铁",
"transportFee": "0.00",
"days": 0.0,
"missionAllowance": "0.00",
"hotelExpense": "0.00",
"urbanTransport": "0.00",
"otherExpense": "0.00",
},
{
"uid": "a8469b44-953a-409a-9c54-156b69931956",
"startDate": "2020-08-01",
"startPoint": "济南",
"arriveDate": "2020-08-01",
"arrivePoint": "济南",
"transport": "高铁",
"transportFee": "0.00",
"days": 0.0,
"missionAllowance": "0.00",
"hotelExpense": "0.00",
"urbanTransport": "0.00",
"otherExpense": "0.00",
},
{
"uid": "5ef4eb56-bc24-4701-8b7e-e2c4c0cd8afd",
"startDate": "2020-08-01",
"startPoint": "石家庄",
"arriveDate": "2020-08-01",
"arrivePoint": "石家庄",
"transport": "高铁",
"transportFee": "4.00",
"days": 0.0,
"missionAllowance": "0.00",
"hotelExpense": "0.00",
"urbanTransport": "0.00",
"otherExpense": "0.00",
},
{
"uid": "134416ff-23a4-45ac-8797-47dc49eeb399",
"startDate": "2020-08-01",
"startPoint": "333",
"arriveDate": "2020-08-01",
"arrivePoint": "4444",
"transport": "高铁",
"transportFee": "0.00",
"days": 0.0,
"missionAllowance": "0.00",
"hotelExpense": "0.00",
"urbanTransport": "0.00",
"otherExpense": "0.00",
},
{
"uid": "affeaccb-1f23-4a22-9816-6ed8739a5437",
"startDate": "2020-08-01",
"startPoint": "常州2",
"arriveDate": "2020-08-01",
"arrivePoint": "衡水2",
"transport": "高铁",
"transportFee": "200.00",
"days": 3.0,
"missionAllowance": "150.00",
"hotelExpense": "500.00",
"urbanTransport": "80.00",
"otherExpense": "0.00",
},
{
"uid": "a0252abe-a8ee-4730-bc56-0af1e081685a",
"startDate": "2020-08-01",
"startPoint": "常州",
"arriveDate": "2020-08-01",
"arrivePoint": "衡水",
"transport": "高铁",
"transportFee": "200.00",
"days": 3.0,
"missionAllowance": "150.00",
"hotelExpense": "500.00",
"urbanTransport": "80.00",
"otherExpense": "0.00",
}
]
}
tpl.render(context)
tpl.save('差旅报销单123123.docx')