-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicReport.py
More file actions
89 lines (73 loc) · 2.83 KB
/
BasicReport.py
File metadata and controls
89 lines (73 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import subprocess
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml import OxmlElement
from docx.oxml import ns
from docx.oxml.ns import qn
from docx.shared import Inches
from docx.table import _Cell
from docx2pdf import convert
class BasicReport:
def __init__(self):
pass
@staticmethod
def generate_pdf_windows(doc_path: str, out_path: str):
"""Generate pdf file for windows system"""
convert(doc_path, out_path)
@staticmethod
def generate_pdf_Linux(doc_path, out_path):
"""Generate pdf file for windows system"""
subprocess.call(['soffice',
# '--headless',
'--convert-to',
'pdf',
'--outdir',
out_path,
doc_path])
return doc_path
@staticmethod
def set_row_height(row, height):
trPr = row.tr.get_or_add_trPr()
trHeight = OxmlElement('w:trHeight')
trHeight.set(qn('w:val'), str(height))
trPr.append(trHeight)
@staticmethod
def set_vertical_cell_direction(cell: _Cell, direction: str):
# direction: tbRl -- top to bottom, btLr -- bottom to top
assert direction in ("tbRl", "btLr")
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
textDirection = OxmlElement('w:textDirection')
textDirection.set(qn('w:val'), direction) # btLr tbRl
tcPr.append(textDirection)
@staticmethod
def create_element(name):
return OxmlElement(name)
@staticmethod
def create_attribute(element, name, value):
element.set(ns.qn(name), value)
def create_document(self, header):
document = Document()
section = document.sections[-1]
section.left_martin = Inches(0.1)
paragraph_format = document.styles['Normal'].paragraph_format
paragraph_format.space_before = 0
paragraph_format.space_after = 0
document.add_paragraph().add_run(header).bold = True
document.add_paragraph(" ")
section.footer.paragraphs[0].text = header
section.footer.add_paragraph()
self.add_page_number(section.footer.paragraphs[1].add_run())
section.footer.paragraphs[1].alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
return document
def add_page_number(self, run):
fldChar1 = self.create_element('w:fldChar')
self.create_attribute(fldChar1, 'w:fldCharType', 'begin')
instrText = self.create_element('w:instrText')
self.create_attribute(instrText, 'xml:space', 'preserve')
instrText.text = "PAGE"
fldChar2 = self.create_element('w:fldChar')
self.create_attribute(fldChar2, 'w:fldCharType', 'end')
run._r.append(fldChar1)
run._r.append(instrText)
run._r.append(fldChar2)