def test_write_only_cell(): from ..dump_worksheet import WriteOnlyCell c = WriteOnlyCell() assert c.parent is None assert c.value is None assert c.column == 'A' assert c.row == 1
def test_append_cell(DumpWorksheet): from ..dump_worksheet import WriteOnlyCell ws = DumpWorksheet cell = WriteOnlyCell(ws, "Hello there") assert ws.parent.shared_strings == [] cell._style = 5 ws.append([cell]) assert ws.parent.shared_strings == ['Hello there'] content = ws.get_temporary_file(ws._fileobj_content_name) content.seek(0) xml = content.read() expected = """<row r="1" spans="1:1"><c r="A1" t="s" s="5"><v>0</v></c></row>""" diff = compare_xml(xml, expected) assert diff is None, diff
def test_dump_with_font(): from openpyxl.writer.dump_worksheet import WriteOnlyCell test_filename = _get_test_filename() wb = Workbook(optimized_write=True) ws = wb.create_sheet() user_style = Style(font=Font(name='Courrier', size=36)) cell = WriteOnlyCell(ws, value='hello') cell.style = Style(font=Font(name='Courrier', size=36)) ws.append([cell, 3.14, None]) assert user_style in wb.shared_styles wb.save(test_filename) wb2 = load_workbook(test_filename) ws2 = wb2[ws.title] assert ws2['A1'].style == user_style
def test_dump_with_comment(): from openpyxl.writer.dump_worksheet import WriteOnlyCell test_filename = _get_test_filename() wb = Workbook(optimized_write=True) ws = wb.create_sheet() user_comment = Comment(text='hello world', author='me') cell = WriteOnlyCell(ws, value="hello") cell.comment = user_comment ws.append([cell, 3.14, None]) assert user_comment in ws._comments wb.save(test_filename) wb2 = load_workbook(test_filename) ws2 = wb2[ws.title] assert ws2['A1'].comment.text == 'hello world'
def test_dump_with_comment(DumpWorksheet): ws = DumpWorksheet from openpyxl.writer.dump_worksheet import WriteOnlyCell from openpyxl.comments import Comment user_comment = Comment(text='hello world', author='me') cell = WriteOnlyCell(ws, value="hello") cell.comment = user_comment ws.append([cell]) assert user_comment in ws._comments ws.write_header() header = ws.get_temporary_file(ws._fileobj_header_name) header.write(b"<sheetData>") # well-formed XML needed ws.close() content = open(ws._fileobj_name).read() expected = """ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <sheetPr> <outlinePr summaryRight="1" summaryBelow="1"/> </sheetPr> <dimension ref="A1:A1"/> <sheetViews> <sheetView workbookViewId="0"> <selection activeCell="A1" sqref="A1"/> </sheetView> </sheetViews> <sheetFormatPr defaultRowHeight="15" baseColWidth="10"/> <sheetData> <row r="1" spans="1:1"><c r="A1" t="s"><v>0</v></c></row> </sheetData> <legacyDrawing r:id="commentsvml"></legacyDrawing> </worksheet> """ diff = compare_xml(content, expected) assert diff is None, diff
def main(comments, output, catalogs): """ Convert .PO files to an XLSX file. po-to-xls tries to guess the locale for PO files by looking at the "Language" key in the PO metadata, falling back to the filename. You can also specify the locale manually by adding prefixing the filename with "<locale>:". For example: "nl:locales/nl/mydomain.po". """ has_msgctxt = False for (locale, catalog) in catalogs: has_msgctxt = has_msgctxt or any(m.msgctxt for m in catalog) fuzzy_font = Font(italic=True, bold=True) messages = [] seen = set() for (_, catalog) in catalogs: for msg in catalog: if not msg.msgid or msg.obsolete: continue if (msg.msgid, msg.msgctxt) not in seen: messages.append((msg.msgid, msg.msgctxt, msg)) seen.add((msg.msgid, msg.msgctxt)) book = openpyxl.Workbook(write_only=True) sheet = book.create_sheet(title=u'Translations') row = [] has_msgctxt_column = has_occurrences_column = has_comment_column = has_tcomment_column = None if has_msgctxt: has_msgctxt_column = True row.append(ColumnHeaders.msgctxt) row.append(ColumnHeaders.msgid) if 'reference' in comments or 'all' in comments: has_occurrences_column = True row.append(ColumnHeaders.occurrences) if 'extracted' in comments or 'all' in comments: has_comment_column = True row.append(ColumnHeaders.comment) if 'translator' in comments or 'all' in comments: has_tcomment_column = True row.append(ColumnHeaders.tcomment) for (i, cat) in enumerate(catalogs): row.append(cat[0]) sheet.append(row) ref_catalog = catalogs[0][1] with click.progressbar(messages, label='Writing catalog to sheet') as todo: for (msgid, msgctxt, message) in todo: row = [] if has_msgctxt_column is not None: row.append(msgctxt) row.append(msgid) msg = ref_catalog.find(msgid, msgctxt=msgctxt) if has_occurrences_column: o = [] if msg is not None: for (entry, lineno) in msg.occurrences: if lineno: o.append(u'%s:%s' % (entry, lineno)) else: o.append(entry) row.append(u', '.join(o) if o else None) if has_comment_column: row.append(msg.comment if msg is not None else None) if has_tcomment_column: row.append(msg.tcomment if msg is not None else None) for cat in catalogs: cat = cat[1] msg = cat.find(msgid, msgctxt=msgctxt) if msg is None: row.append(None) elif 'fuzzy' in msg.flags: cell = WriteOnlyCell(sheet, value=msg.msgstr) cell.font = fuzzy_font row.append(cell) else: row.append(msg.msgstr) sheet.append(row) sheet.freeze_panes = 'B1' book.save(output)