|
|
|
|
@ -1,19 +1,18 @@ |
|
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def copy_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
row_from=0, row_to=None, dst_row_shift=0, |
|
|
|
|
col_from=0, col_to=None, dst_col_shift=0): |
|
|
|
|
def copy_cells(src_sheet, dst_sheet, style_list, row_from=0, row_to=None, dst_row_shift=0, |
|
|
|
|
col_from=0, col_to=None, dst_col_shift=0): |
|
|
|
|
""" |
|
|
|
|
Скопировать блок ячеек из диапазона строк [row_from, row_to] и колонок |
|
|
|
|
[col_from, col_to] исходного листа в новый лист с сохранением их контента, |
|
|
|
|
исходных стилей форматирования, объединения и высоты строк. |
|
|
|
|
""" |
|
|
|
|
row_to = row_to or src_sheet.nrows-1 |
|
|
|
|
col_to = col_to or src_sheet.ncols-1 |
|
|
|
|
row_to = row_to or src_sheet.nrows - 1 |
|
|
|
|
col_to = col_to or src_sheet.ncols - 1 |
|
|
|
|
|
|
|
|
|
for row in range(row_from, row_to+1): |
|
|
|
|
for col in range(col_from, col_to+1): |
|
|
|
|
for row in range(row_from, row_to + 1): |
|
|
|
|
for col in range(col_from, col_to + 1): |
|
|
|
|
cell = src_sheet.cell(row, col) |
|
|
|
|
# скопировать контент и стиль ячейки |
|
|
|
|
dst_sheet.write( |
|
|
|
|
@ -27,26 +26,23 @@ def copy_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
height_rows(src_sheet, dst_sheet, row_from, row_to, dst_row_shift) |
|
|
|
|
|
|
|
|
|
# объединить ячейки |
|
|
|
|
merge_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
row_from, row_to, dst_row_shift, |
|
|
|
|
col_from, col_to, dst_col_shift) |
|
|
|
|
merge_cells(src_sheet, dst_sheet, style_list, row_from, |
|
|
|
|
row_to, dst_row_shift, col_from, col_to, dst_col_shift) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def height_rows(src_sheet, dst_sheet, |
|
|
|
|
row_from=0, row_to=None, dst_row_shift=0): |
|
|
|
|
def height_rows(src_sheet, dst_sheet, row_from=0, row_to=None, dst_row_shift=0): |
|
|
|
|
"""Задать в диапазоне строк [row_from, row_to] высоту как в исходном листе. |
|
|
|
|
""" |
|
|
|
|
row_to = row_to or src_sheet.nrows-1 |
|
|
|
|
for row in range(row_from, row_to+1): |
|
|
|
|
row_to = row_to or src_sheet.nrows - 1 |
|
|
|
|
for row in range(row_from, row_to + 1): |
|
|
|
|
src_rowinfo = src_sheet.rowinfo_map.get(row) |
|
|
|
|
if src_rowinfo: |
|
|
|
|
dst_sheet.row(row+dst_row_shift).height = src_rowinfo.height |
|
|
|
|
dst_sheet.row(row+dst_row_shift).height_mismatch = True |
|
|
|
|
dst_sheet.row(row + dst_row_shift).height = src_rowinfo.height |
|
|
|
|
dst_sheet.row(row + dst_row_shift).height_mismatch = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
row_from=0, row_to=None, dst_row_shift=0, |
|
|
|
|
col_from=0, col_to=None, dst_col_shift=0): |
|
|
|
|
def merge_cells(src_sheet, dst_sheet, style_list, row_from=0, |
|
|
|
|
row_to=None, dst_row_shift=0, col_from=0, col_to=None, dst_col_shift=0): |
|
|
|
|
""" |
|
|
|
|
Объединить ячейки в заданном блоке нового листа, ограниченном строками |
|
|
|
|
[row_from, row_to] и колонками [col_from, col_to], если в исходном листе |
|
|
|
|
@ -55,7 +51,7 @@ def merge_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
row_to = row_to or src_sheet.nrows |
|
|
|
|
col_to = col_to or src_sheet.ncols |
|
|
|
|
|
|
|
|
|
for r1,r2,c1,c2 in src_sheet.merged_cells: |
|
|
|
|
for r1, r2, c1, c2 in src_sheet.merged_cells: |
|
|
|
|
if r1 < row_from or r1 > row_to: |
|
|
|
|
continue |
|
|
|
|
if c1 < col_from or c1 > col_to: |
|
|
|
|
@ -64,23 +60,9 @@ def merge_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
cell = src_sheet.cell(r1, c1) |
|
|
|
|
style = style_list[cell.xf_index] |
|
|
|
|
|
|
|
|
|
# сохранить границы "крайней" ячейки |
|
|
|
|
# нафиг пока эту фичу - из-за нее повылазили какие-то границы, |
|
|
|
|
# которых не было вообще |
|
|
|
|
# brd_1 = style.borders |
|
|
|
|
# cell_2 = src_sheet.cell(r2-1, c2-1) |
|
|
|
|
# brd_2 = style_list[cell_2.xf_index].borders |
|
|
|
|
# print r1,c1,r2,c2, |
|
|
|
|
# print 'borders 1 (left, right, top, bottom)', |
|
|
|
|
# print brd_1.left, brd_1.right, brd_1.top, brd_1.bottom, |
|
|
|
|
# print 'border 2 (same)', |
|
|
|
|
# print brd_2.left, brd_2.right, brd_2.top, brd_2.bottom |
|
|
|
|
# brd_1.right = brd_2.right |
|
|
|
|
# brd_1.right_colour = brd_2.right_colour |
|
|
|
|
|
|
|
|
|
dst_sheet.merge( |
|
|
|
|
r1+dst_row_shift, r2+dst_row_shift-1, |
|
|
|
|
c1+dst_col_shift, c2+dst_col_shift-1, |
|
|
|
|
r1 + dst_row_shift, r2 + dst_row_shift - 1, |
|
|
|
|
c1 + dst_col_shift, c2 + dst_col_shift - 1, |
|
|
|
|
style) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -88,15 +70,15 @@ def width_cols(src_sheet, dst_sheet, col_from=0, col_to=None, dst_col_shift=0): |
|
|
|
|
"""Задать в диапазоне колонок [col_from, col_to] ширину |
|
|
|
|
как в исходном листе. |
|
|
|
|
""" |
|
|
|
|
col_to = col_to or src_sheet.ncols-1 |
|
|
|
|
for col in range(col_from, col_to+1): |
|
|
|
|
dst_sheet.col(col+dst_col_shift).width = ( |
|
|
|
|
col_to = col_to or src_sheet.ncols - 1 |
|
|
|
|
for col in range(col_from, col_to + 1): |
|
|
|
|
dst_sheet.col(col + dst_col_shift).width = ( |
|
|
|
|
src_sheet.computed_column_width(col)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mm_to_twips(x): |
|
|
|
|
"""Перевести из миллиметров в twips.""" |
|
|
|
|
return int(x/25.4*72*20) |
|
|
|
|
return int(x / 25.4 * 72 * 20) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def horz_page_break(dst_sheet, row): |
|
|
|
|
@ -104,25 +86,22 @@ def horz_page_break(dst_sheet, row): |
|
|
|
|
dst_sheet.horz_page_breaks.append((row, 0, 255)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------- прочие хелперы |
|
|
|
|
|
|
|
|
|
def clone_row(src_sheet, dst_sheet, style_list, |
|
|
|
|
src_row, n_times=1, dst_row_shift=0): |
|
|
|
|
def clone_row(src_sheet, dst_sheet, style_list, src_row, n_times=1, dst_row_shift=0): |
|
|
|
|
""" |
|
|
|
|
Размножить n_times раз строку из исходного листа, с сохранением стилей |
|
|
|
|
форматирования. |
|
|
|
|
""" |
|
|
|
|
for offset in range(n_times+1): |
|
|
|
|
for offset in range(n_times + 1): |
|
|
|
|
copy_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
row_from=src_row, row_to=src_row, |
|
|
|
|
dst_row_shift=dst_row_shift+offset) |
|
|
|
|
row_from=src_row, row_to=src_row, |
|
|
|
|
dst_row_shift=dst_row_shift + offset) |
|
|
|
|
|
|
|
|
|
# задать высоту строк |
|
|
|
|
height_rows(src_sheet, dst_sheet, src_row, src_row, dst_row_shift) |
|
|
|
|
|
|
|
|
|
# объединить ячейки |
|
|
|
|
merge_cells(src_sheet, dst_sheet, style_list, |
|
|
|
|
src_row, src_row, dst_row_shift) |
|
|
|
|
src_row, src_row, dst_row_shift) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_cells_in_row(src_sheet, dst_sheet, style_list, src_row, dst_row): |
|
|
|
|
@ -130,19 +109,20 @@ def merge_cells_in_row(src_sheet, dst_sheet, style_list, src_row, dst_row): |
|
|
|
|
Объединить ячейки в заданной строке нового листа, если в исходном листе они |
|
|
|
|
были объединены, с сохранением исходных стилей форматирования. |
|
|
|
|
""" |
|
|
|
|
for r1,r2,c1,c2 in src_sheet.merged_cells: |
|
|
|
|
for r1, r2, c1, c2 in src_sheet.merged_cells: |
|
|
|
|
if r1 != src_row: |
|
|
|
|
continue |
|
|
|
|
cell = src_sheet.cell(r1, c1) |
|
|
|
|
dst_sheet.merge(dst_row, dst_row, c1, c2-1, style_list[cell.xf_index]) |
|
|
|
|
dst_sheet.merge(dst_row, dst_row, c1, c2 - 1, style_list[cell.xf_index]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sum_src_heights(src_sheet, row_from, row_to): |
|
|
|
|
"""Суммарная высота всех строк диапазона [row_from, row_to] |
|
|
|
|
""" |
|
|
|
|
Суммарная высота всех строк диапазона [row_from, row_to] |
|
|
|
|
исходного листа. |
|
|
|
|
""" |
|
|
|
|
result = 0 |
|
|
|
|
for row in range(row_from, row_to+1): |
|
|
|
|
for row in range(row_from, row_to + 1): |
|
|
|
|
src_rowinfo = src_sheet.rowinfo_map.get(row) |
|
|
|
|
if src_rowinfo: |
|
|
|
|
result += src_rowinfo.height |
|
|
|
|
@ -150,10 +130,11 @@ def sum_src_heights(src_sheet, row_from, row_to): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sum_dst_heights(dst_sheet, row_from, row_to): |
|
|
|
|
"""Суммарная высота всех строк диапазона [row_from, row_to] |
|
|
|
|
""" |
|
|
|
|
Суммарная высота всех строк диапазона [row_from, row_to] |
|
|
|
|
на новом листе. |
|
|
|
|
""" |
|
|
|
|
result = 0 |
|
|
|
|
for row in range(row_from, row_to+1): |
|
|
|
|
for row in range(row_from, row_to + 1): |
|
|
|
|
result += dst_sheet.row(row).height |
|
|
|
|
return result |
|
|
|
|
|