python修改word文档的域_使用python-docx-template修改word文档

项目依赖1:python-docx

项目依赖2:jinja

pip install docxtpl

模板:

1385177-20200808104234350-1087112556.png 效果:

1385177-20200808104420491-340423426.png

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')

1385177-20200808112722448-162315683.png

1385177-20200808112804481-1482848713.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值