1.在settings.py 文件中。加入如下语句
MEDIA_URL='/doctemplates/' #模板的路径名称
MEDIA_ROOT = os.path.join(BASE_DIR, 'doctemplates')#模板的路径
2.在工程下面建立
doctemplates 文件夹。用于存放模板文件。如testTemplate.docx
模板里面的内容如下
资证明
员工 {{ text }} 在我公司单位员工,身份中号为{{id_card}}.请了解并证明
XXXXX公司
3.安装包 docxtpl .
4.书写的文字如下
from Day72bbs.settings import MEDIA_ROOT
from django.shortcuts import render
from docxtpl import DocxTemplate
def post(self,request):
base_url = MEDIA_ROOT
mubanurl= base_url+r' estTemplate.docx'
# print(base_url)
tpl = DocxTemplate(mubanurl)
content = {'text': "我是一个好人啊", 'ic_card': '123456789098765432345'}
tpl.render(content)
tpl.save(base_url + "证明.docx")
return render(request, "zhengming.html", content)
if __name__ == '__main__':
obj = post('s','www.163.com')
————恢复内容开始————
1.pip install pypiwin32 安装模块
2.创建类:printers.py
import tempfile
import win32api
import win32print
def printer_loading(filename):
open(filename, "r")
win32api.ShellExecute(
0,
"print",
filename,
#
# If this is None,the default printer will
# be used anyway
#
'/d:"%s"' % win32print.GetDefaultPrinter(),
'.',
0
)
3.在其它模块中引用
import os
from App01 import printers
path = base_url+r'zhengming'
for a,b,c in os.walk(path):
# print('a'*100)
# print(a)
# print('b'*100)
# print(b)
# print('c'*100)
print(c)
for i in c:
f = os.path.join(path,i)
if f.endswith("docx"):
printers.printer_loading(f)
————恢复内容结束————
最新评论