You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
289 lines
12 KiB
Python
289 lines
12 KiB
Python
import os
|
|
import sys
|
|
import platform
|
|
|
|
libpath = os.path.dirname(os.path.abspath(__file__))
|
|
libpath += '/lib'
|
|
#sysstr = platform.system()
|
|
#if(sysstr =="Windows"):
|
|
# libpath += '/win'
|
|
#else:
|
|
# libpath += '/linux'
|
|
|
|
if (os.path.exists(libpath)):
|
|
sys.path.append(libpath)
|
|
|
|
# 导入库操作
|
|
from docx import Document
|
|
# 导入英寸单位操作(可用于指定图片大小、表格宽高等)
|
|
import docx.shared
|
|
from docx.shared import Cm, Emu, Pt,Mm,Inches
|
|
|
|
#style
|
|
import re
|
|
from docx.shared import Pt,RGBColor
|
|
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
|
from docx.oxml import OxmlElement
|
|
from docx.oxml.ns import qn
|
|
|
|
class Report:
|
|
def __init__(self, path):
|
|
self.create(path)
|
|
self.path = path
|
|
|
|
def create(self, path:str):
|
|
if (os.path.exists(path)):
|
|
os.remove(path)
|
|
document = Document()
|
|
self.doc = document
|
|
|
|
def addTable(self, row, column, dataset):
|
|
table = self.doc.add_table(row, column) #新建表格
|
|
for rowdata in dataset: #自动填充到表格
|
|
row_cells = table.row_cells(dataset.index(rowdata))
|
|
for i in range(column):
|
|
row_cells[i].text = rowdata[i]
|
|
|
|
def addPicture(self, picList, layout, width, height):
|
|
document = self.doc
|
|
#document.add_picture(picPath, width, height)
|
|
run = document.add_paragraph().add_run()
|
|
totalW = document.sections[-1].page_width.mm - document.sections[-1].left_margin.mm - document.sections[-1].right_margin.mm
|
|
|
|
w = totalW / layout
|
|
h = height
|
|
if (w < width):
|
|
h = w / width * height
|
|
print(w)
|
|
print(picList)
|
|
for pic in picList:
|
|
run.add_picture(pic, Mm(w), Mm(h))
|
|
|
|
def addTitle(self, str, level):
|
|
self.doc.add_heading(str, level)
|
|
|
|
def addParagraph(self, str):
|
|
self.doc.add_paragraph(str)
|
|
|
|
def setTOC(self):
|
|
paragraph = self.doc.add_paragraph()
|
|
run = paragraph.add_run()
|
|
|
|
fldChar = OxmlElement('w:fldChar') # creates a new element
|
|
fldChar.set(qn('w:fldCharType'), 'begin') # sets attribute on element
|
|
instrText = OxmlElement('w:instrText')
|
|
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element
|
|
instrText.text = 'TOC \\o "1-3" \\h \\z \\u' # change 1-3 depending on heading levels you need
|
|
|
|
fldChar2 = OxmlElement('w:fldChar')
|
|
fldChar2.set(qn('w:fldCharType'), 'separate')
|
|
|
|
# fldChar3 = OxmlElement('w:t')
|
|
fldChar3 = OxmlElement('w:updateFields')
|
|
fldChar3.set(qn('w:val'), 'true')
|
|
fldChar3.text = "Right-click to update field."
|
|
fldChar2.append(fldChar3)
|
|
|
|
fldChar4 = OxmlElement('w:fldChar')
|
|
fldChar4.set(qn('w:fldCharType'), 'end')
|
|
|
|
r_element = run._r
|
|
r_element.append(fldChar)
|
|
r_element.append(instrText)
|
|
r_element.append(fldChar2)
|
|
r_element.append(fldChar4)
|
|
p_element = paragraph._p
|
|
|
|
# 添加分页符
|
|
paragraph.add_run().add_break(docx.enum.text.WD_BREAK.PAGE)
|
|
|
|
def writeDoc(self):
|
|
self.doc.save(self.path)
|
|
|
|
def set_updatefields_true(self, docx_path):
|
|
namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
|
|
f = open(docx_path, 'rb')
|
|
doc = Document(f)
|
|
# add child to doc.settings element
|
|
element_updatefields = lxml.etree.SubElement(
|
|
doc.settings.element, f"{namespace}updateFields"
|
|
)
|
|
element_updatefields.set(f"{namespace}val", "true")
|
|
doc.save(docx_path) ## Heading ##
|
|
|
|
class docmStyle:
|
|
def __init__(self, path):
|
|
self.path = path
|
|
self.doc = Document(path)
|
|
|
|
def zhengwen(self):
|
|
for paragraph in self.doc.paragraphs:
|
|
if re.match('^Heading 1',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(16)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Heading 2',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(14)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Heading 3',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(12)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Heading 4',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(12)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Heading 5',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(12)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Heading 6',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(12)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Heading 7',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(12)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
elif re.match('^Title',paragraph.style.name):
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
paragraph.paragraph_format.space_before = Pt(5)
|
|
paragraph.paragraph_format.space_after = Pt(5)
|
|
paragraph.paragraph_format.first_line_indent = Cm(0)
|
|
paragraph.paragraph_format.left_indent = Cm(0)
|
|
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(16)
|
|
run.font.name = u'黑体'
|
|
run.font.color.rgb = RGBColor(0,0,0)
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
run.font.bold = True
|
|
#目录
|
|
elif re.match('^toc',paragraph.style.name):
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(10.5)
|
|
run.font.name = u'宋体'
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
|
|
run.font.italic = False
|
|
run.font.bold = False
|
|
#图 表
|
|
else:
|
|
if re.match("(表)",paragraph.text):
|
|
print(paragraph.text)
|
|
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(9)
|
|
run.font.name = u'黑体'
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
|
|
elif re.match("(图)",paragraph.text):
|
|
print(paragraph.text)
|
|
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(9)
|
|
run.font.name = u'黑体'
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
|
|
#正文
|
|
else:
|
|
paragraph.paragraph_format.line_spacing = 1.5
|
|
for run in paragraph.runs:
|
|
run.font.size = Pt(10.5)
|
|
run.font.name = u'宋体'
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
|
|
run.font.italic = False
|
|
run.font.bold = False
|
|
|
|
|
|
def biaoge(self):
|
|
document = self.doc
|
|
tables=document.tables
|
|
for tb in document.tables:
|
|
for row in tb.rows:
|
|
for cell in row.cells:
|
|
for par in cell.paragraphs:
|
|
for run in par.runs:
|
|
run.font.size = Pt(9)
|
|
run.font.name ='宋体'
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
|
|
|
|
|
|
def modifyStyle(self):
|
|
self.zhengwen()
|
|
self.biaoge()
|
|
self.doc.save(self.path)
|
|
|
|
|
|
#def test():
|
|
# report = Report('text.docx')
|
|
# report.setTOC()
|
|
# report.addTitle('Title",0)
|
|
# report.addTitle("Head1",0)
|
|
# report.addTitle("Head2",2)
|
|
# report.addParagraph("paragraph")
|
|
# dataSet = [['a','b','c','d'],['1','2','3','4'],['5','6','7','8'],['9','10','11','12']]
|
|
# report.addTable(4,4,dataSet)
|
|
# # listPic = ['test.jpg', 'test.jpg', 'test.jpg', 'test.jpg']
|
|
# # report.addPicture(listPic, 2, 1626,696)
|
|
# report.writeDoc()
|
|
|
|
# docstyle = docmStyle('text.docx')
|
|
# docstyle.modifyStyle()
|