You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.4 KiB

from docx import Document
from docx.shared import Pt, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_UNDERLINE
from docx import Document
from docx.shared import Inches
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
import time
from io import BytesIO
import os
#设置表格的边框
def set_cell_border(cell, **kwargs):
"""
Set cell`s border
Usage:
set_cell_border(
cell,
top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
bottom={"sz": 12, "color": "#00FF00", "val": "single"},
left={"sz": 24, "val": "dashed", "shadow": "true"},
right={"sz": 12, "val": "dashed"},
)
"""
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# check for tag existnace, if none found, then create one
tcBorders = tcPr.first_child_found_in("w:tcBorders")
if tcBorders is None:
tcBorders = OxmlElement('w:tcBorders')
tcPr.append(tcBorders)
# list over all available tags
for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):
edge_data = kwargs.get(edge)
if edge_data:
tag = 'w:{}'.format(edge)
# check for tag existnace, if none found, then create one
element = tcBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tcBorders.append(element)
# looks like order of attributes is important
for key in ["sz", "val", "color", "space", "shadow"]:
if key in edge_data:
element.set(qn('w:{}'.format(key)), str(edge_data[key]))
class WriteDoc(object):
def __init__(self, data, path):
self.data = data
self.path = path
self.d = Document()
section = self.d.sections[0]
header = section.header
paragraph = header.paragraphs[0]
paragraph.paragraph_format.left_indent = 0
paragraph.paragraph_format.right_indent = 0
now_date = time.strftime("%Y-%m-%d", time.localtime())
text=paragraph.add_run('记录格式编号 : 实施日期 :{} 版本号/修改号 :'.format(now_date))
text.font.size = Pt(6)
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
# paragraph.runs[-1].underline = WD_UNDERLINE.THICK#页眉下划线
#添加列表
def add_table(self):
for i in range(len(self.data)):
table = self.d.add_table(rows=11, cols=2, style="Table Grid")
self.merge_table(table)
self.write_data(table,i)
self.d.add_paragraph("").paragraph_format.space_after = Pt(25)
return self
#写入数据
def write_data(self,table,index):
# for i in range(len(self.data)):
# table =Document.tables[i]
i=self.data[index]
try:
cell = table.cell(0, 0)
cell.text = "标准储备液名称:{}".format(i.name)
cell = table.cell(0, 1)
cell.text = "编号:{}".format(i.seq_no)
cell = table.cell(1, 0)
cell.text = "溶剂:{}".format(i.solvent)
cell = table.cell(2, 0)
cell.text = "配制日期:{}".format(i.configuration_date)
cell = table.cell(2, 1)
cell.text = "有效期:{}".format(i.validity_date)
cell = table.cell(3, 0)
cell.text = "配制浓度:{}".format(i.config_concentration)
cell = table.cell(3, 1)
cell.text = "配制量:{}".format(i.allocation)
cell = table.cell(4, 0)
cell.text = "配制依据:{}".format(i.configuration_basis)
cell = table.cell(5, 0)
cell.text = "单位编号:{}".format(i.barcode_list)
cell = table.cell(6, 0)
cell.text = "配制记录:{}".format(i.configuration_record)
cell = table.cell(10, 0)
set_cell_border(
cell,
top={"sz": 12, "val": "single", "color": "white", "space": "0"},
right={"sz": 12, "val": "single", "color": "white", "space": "0"},
)
cell = table.cell(10, 1)
set_cell_border(
cell,
top={"sz": 12, "val": "single", "color": "white", "space": "0"},
)
cell.text = "配制人:{}".format(i.user_name)
except Exception as e:
print('')
# 保存文件
def save(self, file_path):
self.d.save(file_path)
# 关闭文件
def close(self):
self.d.close()
#合并单元格,修改列宽
def merge_table(self,table):
table.cell(1,0).merge(table.cell(1,1))
table.cell(4,0).merge(table.cell(4,1))
table.cell(5,0).merge(table.cell(5,1))
table.cell(6,0).merge(table.cell(9,1))
table.cell(0,0).width=Inches(6.5)
# 下载文件
@staticmethod
def download_doc(filename, chunk_size=512):
try:
f = Document(filename)
b=BytesIO()
f.save(b)
b.flush()
b.seek(0)
b_data=b.getvalue()
b.close()
os.remove(filename)
return b_data
except:
return ''
if __name__ == '__main__':
pass
# WriteDoc([1,2,3,4,5,6,7,8,9],'').add_table()