Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/comments/comment_sheet.py: 56%
115 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1# Copyright (c) 2010-2022 openpyxl
3## Incomplete!
4from openpyxl.descriptors.serialisable import Serialisable
5from openpyxl.descriptors import (
6 Typed,
7 Float,
8 Integer,
9 Set,
10 String,
11 Bool,
12)
13from openpyxl.descriptors.excel import Guid, ExtensionList
14from openpyxl.descriptors.sequence import NestedSequence
16from openpyxl.utils.indexed_list import IndexedList
17from openpyxl.xml.constants import SHEET_MAIN_NS
18from openpyxl.xml.functions import tostring
20from openpyxl.cell.text import Text
21#from openpyxl.worksheet.ole import ObjectAnchor
22from .author import AuthorList
23from .comments import Comment
24from .shape_writer import ShapeWriter
27class Properties(Serialisable):
29 locked = Bool(allow_none=True)
30 defaultSize = Bool(allow_none=True)
31 _print = Bool(allow_none=True)
32 disabled = Bool(allow_none=True)
33 uiObject = Bool(allow_none=True)
34 autoFill = Bool(allow_none=True)
35 autoLine = Bool(allow_none=True)
36 altText = String(allow_none=True)
37 textHAlign = Set(values=(['left', 'center', 'right', 'justify', 'distributed']))
38 textVAlign = Set(values=(['top', 'center', 'bottom', 'justify', 'distributed']))
39 lockText = Bool(allow_none=True)
40 justLastX = Bool(allow_none=True)
41 autoScale = Bool(allow_none=True)
42 rowHidden = Bool(allow_none=True)
43 colHidden = Bool(allow_none=True)
44 #anchor = Typed(expected_type=ObjectAnchor, )
46 __elements__ = ('anchor',)
48 def __init__(self,
49 locked=None,
50 defaultSize=None,
51 _print=None,
52 disabled=None,
53 uiObject=None,
54 autoFill=None,
55 autoLine=None,
56 altText=None,
57 textHAlign=None,
58 textVAlign=None,
59 lockText=None,
60 justLastX=None,
61 autoScale=None,
62 rowHidden=None,
63 colHidden=None,
64 anchor=None,
65 ):
66 self.locked = locked
67 self.defaultSize = defaultSize
68 self._print = _print
69 self.disabled = disabled
70 self.uiObject = uiObject
71 self.autoFill = autoFill
72 self.autoLine = autoLine
73 self.altText = altText
74 self.textHAlign = textHAlign
75 self.textVAlign = textVAlign
76 self.lockText = lockText
77 self.justLastX = justLastX
78 self.autoScale = autoScale
79 self.rowHidden = rowHidden
80 self.colHidden = colHidden
81 self.anchor = anchor
84class CommentRecord(Serialisable):
86 tagname = "comment"
88 ref = String()
89 authorId = Integer()
90 guid = Guid(allow_none=True)
91 shapeId = Integer(allow_none=True)
92 text = Typed(expected_type=Text)
93 commentPr = Typed(expected_type=Properties, allow_none=True)
94 author = String(allow_none=True)
96 __elements__ = ('text', 'commentPr')
97 __attrs__ = ('ref', 'authorId', 'guid', 'shapeId')
99 def __init__(self,
100 ref="",
101 authorId=0,
102 guid=None,
103 shapeId=0,
104 text=None,
105 commentPr=None,
106 author=None,
107 height=79,
108 width=144
109 ):
110 self.ref = ref
111 self.authorId = authorId
112 self.guid = guid
113 self.shapeId = shapeId
114 if text is None:
115 text = Text()
116 self.text = text
117 self.commentPr = commentPr
118 self.author = author
119 self.height = height
120 self.width = width
123 @classmethod
124 def from_cell(cls, cell):
125 """
126 Class method to convert cell comment
127 """
128 comment = cell._comment
129 ref = cell.coordinate
130 self = cls(ref=ref, author=comment.author)
131 self.text.t = comment.content
132 self.height = comment.height
133 self.width = comment.width
134 return self
137 @property
138 def content(self):
139 """
140 Remove all inline formatting and stuff
141 """
142 return self.text.content
145class CommentSheet(Serialisable):
147 tagname = "comments"
149 authors = Typed(expected_type=AuthorList)
150 commentList = NestedSequence(expected_type=CommentRecord, count=0)
151 extLst = Typed(expected_type=ExtensionList, allow_none=True)
153 _id = None
154 _path = "/xl/comments/comment{0}.xml"
155 mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml"
156 _rel_type = "comments"
157 _rel_id = None
159 __elements__ = ('authors', 'commentList')
161 def __init__(self,
162 authors=None,
163 commentList=None,
164 extLst=None,
165 ):
166 self.authors = authors
167 self.commentList = commentList
170 def to_tree(self):
171 tree = super(CommentSheet, self).to_tree()
172 tree.set("xmlns", SHEET_MAIN_NS)
173 return tree
176 @property
177 def comments(self):
178 """
179 Return a dictionary of comments keyed by coord
180 """
181 authors = self.authors.author
183 for c in self.commentList:
184 yield c.ref, Comment(c.content, authors[c.authorId], c.height, c.width)
187 @classmethod
188 def from_comments(cls, comments):
189 """
190 Create a comment sheet from a list of comments for a particular worksheet
191 """
192 authors = IndexedList()
194 # dedupe authors and get indexes
195 for comment in comments:
196 comment.authorId = authors.add(comment.author)
198 return cls(authors=AuthorList(authors), commentList=comments)
201 def write_shapes(self, vml=None):
202 """
203 Create the VML for comments
204 """
205 sw = ShapeWriter(self.comments)
206 return sw.write(vml)
209 @property
210 def path(self):
211 """
212 Return path within the archive
213 """
214 return self._path.format(self._id)