def add_rows_of_values(self, rows): # From a list of values, for each value # Duplicate all rows and append that value to one set of rows values = [xstr(val) for val in rows] num = len(values) if num == 0: self.add_value('') return self if num == 1: self.add_value(values[0]) return self if self.split: oldrows = self.rows[:] # Make a copy of old rows num times for i in range(1, num): for j, row in enumerate(oldrows): self.rows.insert(i * len(oldrows) + j, row.copy()) # Append each value to corresponding set of rows for i in range(0, num): for j in range(0, len(oldrows)): self.rows[i * len(oldrows) + j].append(values[i]) self.group_rows.append(', '.join(values)) return self
def add_hyperlink(self, url, text): part = self.ref.part r_id = part.relate_to( url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True, ) hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink') hyperlink.set(docx.oxml.shared.qn('r:id'), r_id) new_run = docx.oxml.shared.OxmlElement('w:r') r_pr = docx.oxml.shared.OxmlElement('w:rPr') new_run.append(r_pr) new_run.text = xstr(text) hyperlink.append(new_run) self.ref._p.append(hyperlink) r = docx.text.run.Run(new_run, self.ref) r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK r.font.underline = True return self
def add_value(self, value): val = xstr(value) if self.split: # Add single value to end of each row [row.append(val) for row in self.rows] self.group_rows.append(val) return self
def add_rows_of_value_lists(self, rows, col_span=1): # From a list of lists, for each list # Duplicate all rows and append each value of # that list to one set of rows values = [] for row in rows: values.append([xstr(val) for val in row]) num = len(values) if num == 0: self.add_value_list([''] * col_span) return self if num == 1: self.add_value_list(values[0]) return self if self.split: oldrows = self.rows[:] # Make a copy of old rows num times for i in range(1, num): for j, row in enumerate(oldrows): self.rows.insert(i * len(oldrows) + j, row.copy()) # Append each list of values to corresponding set of rows for i in range(0, num): for j in range(0, len(oldrows)): for k in range(0, len(values[i])): self.rows[i * len(oldrows) + j].append(values[i][k]) # Zip to group together elements of same column, # Convert each zipped to list and convert overall to list as well for column in list(map(list, zip(*values))): # Make sure each column only contains unique values self.group_rows.append(', '.join( # sorted(list(dict.fromkeys(column))) list(OrderedDict.fromkeys(column)) )) return self
def get_hyperlink(url, text): clean_text = xstr(text.replace('"', '""')) return f'=HYPERLINK("{url}", "{clean_text}")'
def add_run(self, text=None): return Run(self.ref.add_run(xstr(text)))
def add_text(self, text): self.ref.add_text(xstr(text)) return self
def add_paragraph(self, text=None): return Paragraph(self.doc.add_paragraph(xstr(text)))