11
22from PIL import Image # type: ignore
3- from docx import Document # type: ignore
3+ import docx # type: ignore
4+ import docx .document # type: ignore
45from docx .shared import Cm , Pt # type: ignore
56from docx .enum .dml import MSO_THEME_COLOR_INDEX as MSO_THEME_COLOR # type: ignore
67from docx .enum .section import WD_SECTION_START # type: ignore
1516import Danny .OOo .OOoLib
1617from Danny .OOo import OOoLib as ooo
1718from Danny .OOo .OOoLib import makePropertyValue
18- from docx .shared import RGBColor
19+ from docx .shared import RGBColor # type: ignore
1920
2021ref_t = Callable [[str ], str ]
2122
2223TABLE_STD_STYLE = "Table Grid"
2324
2425class IDocxParRenderer (object , metaclass = abc .ABCMeta ):
2526 @abc .abstractmethod
26- def render (self , p : Paragraph , doc : Document ) -> None :
27+ def render (self , p : Paragraph , doc : docx . document . Document ) -> None :
2728 raise NotImplementedError ('users must define render to use this base class' )
2829
2930class DocxEntityParagraphNormal (IDocxParRenderer ):
3031 def __init__ (self , text : str , style : str = None ) -> None :
3132 self .text , self .style = text , style
32- def render (self , p : Paragraph , _ : Document ) -> None :
33+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
3334 p .add_run (self .text , self .style )
3435
3536class DocxEntityParagraphItalic (IDocxParRenderer ):
3637 def __init__ (self , text : str , style : str = None ) -> None :
3738 self .text , self .style = text , style
38- def render (self , p : Paragraph , _ : Document ) -> None :
39+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
3940 p .add_run (self .text , self .style ).italic = True
4041
4142class DocxEntityParagraphUnderline (IDocxParRenderer ):
4243 def __init__ (self , text : str , style : str = None ) -> None :
4344 self .text , self .style = text , style
44- def render (self , p : Paragraph , _ : Document ) -> None :
45+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
4546 p .add_run (self .text , self .style ).underline = True
4647
4748class DocxEntityParagraphColor (IDocxParRenderer ):
4849 def __init__ (self , text : str , color : RGBColor , style : str = None ) -> None :
4950 self .text , self .style , self .color = text , style , color
50- def render (self , p : Paragraph , _ : Document ) -> None :
51+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
5152 p .add_run (self .text , self .style ).font .color .rgb = self .color
5253
5354class DocxEntityParagraphBold (IDocxParRenderer ):
5455 def __init__ (self , text : str , style : str = None ) -> None :
5556 self .text , self .style = text , style
56- def render (self , p : Paragraph , _ : Document ) -> None :
57+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
5758 p .add_run (self .text , self .style ).bold = True
5859
5960class DocxEntityParagraphFont (IDocxParRenderer ):
6061 def __init__ (self , text : str , font_name :str , font_size :int , style : str = None ) -> None :
6162 self .text , self .style = text , style
6263 self .font_name , self .font_size = font_name , font_size
63- def render (self , p : Paragraph , _ : Document ) -> None :
64+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
6465 font = p .add_run (self .text , self .style ).font
6566 font .name = self .font_name
6667 font .size = Pt (self .font_size )
6768
6869class DocxEntityParagraphItem (IDocxParRenderer ):
6970 def __init__ (self , text : str , style : str = "List Paragraph" ) -> None :
7071 self .text , self .style = text , style
71- def render (self , _ : Paragraph , doc : Document ) -> None :
72+ def render (self , _ : Paragraph , doc : docx . document . Document ) -> None :
7273 doc .add_paragraph (self .text , self .style )
7374
7475class DocxEntityParagraphRef (IDocxParRenderer ):
7576 def __init__ (self , ref : ref_t , key : str ) -> None :
7677 self .key , self .ref = key , ref
77- def render (self , p : Paragraph , _ : Document ) -> None :
78+ def render (self , p : Paragraph , _ : docx . document . Document ) -> None :
7879 if self .ref :
7980 p .add_run (self .ref (self .key ))
8081
8182class IDocxEntityRenderer (object , metaclass = abc .ABCMeta ):
8283 @abc .abstractmethod
83- def render (self , doc : Document ):
84+ def render (self , doc : docx . document . Document ):
8485 raise NotImplementedError ('users must define render to use this base class' )
8586
8687class DocxEntityParagraph (IDocxEntityRenderer ):
@@ -98,7 +99,7 @@ def u(self, text: str, style: str=None) -> 'DocxEntityParagraph':
9899 self .subs .append (DocxEntityParagraphUnderline (text , style ))
99100 return self
100101 def color (self , text : str , r :int = 0 , g :int = 0 , b :int = 0 , style : str = None ) -> 'DocxEntityParagraph' :
101- self .subs .append (DocxEntityParagraphColor (text , RGBColor (r ,g ,b ), style ))
102+ self .subs .append (DocxEntityParagraphColor (text , RGBColor (r ,g ,b ), style )) # type: ignore
102103 return self
103104 def font (self , text :str = "" , font_name :str = "Calibri" , font_size :int = 12 , style :str = None ) -> 'DocxEntityParagraph' :
104105 self .subs .append (DocxEntityParagraphFont (text , font_name , font_size , style ))
@@ -114,7 +115,7 @@ def item(self, text: str="", style: str="Bullet0") -> 'DocxEntityParagraph':
114115 def ref (self , key : str ) -> 'DocxEntityParagraph' :
115116 self .subs .append (DocxEntityParagraphRef (self ._ref , key ))
116117 return self
117- def render (self , doc : Document ) -> None :
118+ def render (self , doc : docx . document . Document ) -> None :
118119 p = doc .add_paragraph (self .text , self .style )
119120 for obj in self .subs :
120121 obj .render (p , doc )
@@ -130,7 +131,7 @@ def __init__(self, filename: str, width: Cm = DEFAULT_WIDTH, height : Cm= None,
130131 self .width , self .height , self .filename , self .caption = width , height , filename , caption
131132 self .key = "?"
132133
133- def render (self , doc : Document ) -> None :
134+ def render (self , doc : docx . document . Document ) -> None :
134135 imagePath = 'images/' + self .filename
135136
136137 # autosize en fonction de l'orientation
@@ -164,7 +165,7 @@ def render(self, doc: Document) -> None:
164165class DocsEntityPageSection (IDocxEntityRenderer ):
165166 def __init__ (self , start_type = WD_SECTION_START .NEW_PAGE , orientation = WD_ORIENTATION .PORTRAIT ): # @UndefinedVariable pylint: disable=no-member
166167 self .start_type , self .orientation = start_type , orientation
167- def render (self , doc : Document ) -> None :
168+ def render (self , doc : docx . document . Document ) -> None :
168169 last_section = doc .sections [- 1 ]
169170 last_orientation = last_section .orientation
170171 s = doc .add_section (self .start_type )
@@ -173,7 +174,7 @@ def render(self, doc: Document) -> None:
173174 s .orientation , s .page_height , s .page_width = self .orientation , new_height , new_width
174175
175176class DocsEntityPageBreak (IDocxEntityRenderer ):
176- def render (self , doc : Document ) -> None :
177+ def render (self , doc : docx . document . Document ) -> None :
177178 doc .add_page_break ()
178179
179180class DocxEntityDocumentTitle (IDocxEntityRenderer ):
@@ -215,7 +216,7 @@ class DocxEntityTable((IDocxEntityRenderer)):
215216 def __init__ (self , callback : TableConstructor , rows : int , cols : int , caption : str = None , style : str = None ) -> None :
216217 self .callback , self .caption , self .rows , self .cols , self .style = callback , caption , rows , cols , style
217218 self .key = "?"
218- def render (self , doc : Document ) -> None :
219+ def render (self , doc : docx . document . Document ) -> None :
219220 table = doc .add_table (self .rows , self .cols , self .style )
220221 self .callback .render (table )
221222 if self .caption :
@@ -224,7 +225,7 @@ def render(self, doc: Document) -> None:
224225class DocxEntityTOC (IDocxEntityRenderer ):
225226 def __init__ (self , titre :str , command :str ) -> None :
226227 self .command , self .titre = command , titre
227- def render (self , doc : Document ) -> None :
228+ def render (self , doc : docx . document . Document ) -> None :
228229 if self .titre :
229230 doc .add_paragraph (self .titre , "Illustration Index Heading" )
230231 paragraph = doc .add_paragraph ()
@@ -263,7 +264,7 @@ def append(self, obj: IDocxEntityRenderer) -> IDocxEntityRenderer:
263264 self .entities .append (obj )
264265 return obj
265266
266- def initialize (self , doc : Document ) -> None :
267+ def initialize (self , doc : docx . document . Document ) -> None :
267268 # caption defaults
268269 style = doc .styles ['Caption' ]
269270 #font = style.font
@@ -300,7 +301,7 @@ def initialize(self, doc: Document) -> None:
300301 paragraph_format .space_before = Pt (6 )
301302 paragraph_format .space_after = Pt (12 )
302303
303- def render (self , doc : Document ) -> None :
304+ def render (self , doc : docx . document . Document ) -> None :
304305 self .initialize (doc )
305306 for obj in self .entities :
306307 obj .render (doc )
@@ -353,13 +354,13 @@ def subsec(self, title: str, style: str=None) -> DocxEntitySubSection:
353354 def subsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSection :
354355 return cast (DocxEntitySubSubSection , self .entity .append (DocxEntitySubSubSection (title , style )))
355356
356- def subsubsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSection :
357+ def subsubsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSubSection :
357358 return cast (DocxEntitySubSubSubSection , self .entity .append (DocxEntitySubSubSubSection (title , style )))
358359
359- def subsubsubsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSection :
360+ def subsubsubsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSubSubSection :
360361 return cast (DocxEntitySubSubSubSubSection , self .entity .append (DocxEntitySubSubSubSubSection (title , style )))
361362
362- def subsubsubsubsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSection :
363+ def subsubsubsubsubsec (self , title :str , style :str = None ) -> DocxEntitySubSubSubSubSubSection :
363364 return cast (DocxEntitySubSubSubSubSubSection , self .entity .append (DocxEntitySubSubSubSubSubSection (title , style )))
364365
365366 def pic (self , filename : str , width :Cm = DocxEntityPicture .DEFAULT_WIDTH , height :Cm = None , caption :str = None ) -> DocxEntityPicture :
@@ -399,11 +400,11 @@ def pageSection(self, start_type: 'START_TYPE' = START_TYPE.NEW_PAGE, orientatio
399400 def table (self , callback : TableConstructor , rows : int , cols : int , caption = None , style = None ) -> DocxEntityTable :
400401 return cast (DocxEntityTable , self .entity .append (DocxEntityTable (callback , rows , cols , caption , style )))
401402
402- def save (self , target :str = None , pre : Callable [[Document ],None ]= None ) -> None :
403+ def save (self , target :str = None , pre : Callable [[docx . document . Document ],None ]= None ) -> None :
403404 if self .filename :
404- d = Document (self .filename )
405+ d = docx . Document (self .filename )
405406 else :
406- d = Document ()
407+ d = docx . Document ()
407408 #for i in d.styles:
408409 # print(i)
409410 if pre :
@@ -423,7 +424,7 @@ def export_pdf(self, target:str=None, source:str=None):
423424 if self .filename :
424425 path = self .filename
425426 else :
426- raise "Error source filename"
427+ raise BaseException ( "Error source filename" )
427428 if target :
428429 target_path = target
429430 else :
0 commit comments