You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
6.1 KiB
158 lines
6.1 KiB
# -*- 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):
|
|
"""
|
|
Скопировать блок ячеек из диапазона строк [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
|
|
|
|
for row in xrange(row_from, row_to+1):
|
|
for col in xrange(col_from, col_to+1):
|
|
cell = src_sheet.cell(row, col)
|
|
# скопировать контент и стиль ячейки
|
|
dst_sheet.write(
|
|
row + dst_row_shift,
|
|
col + dst_col_shift,
|
|
cell.value,
|
|
style_list[cell.xf_index],
|
|
)
|
|
|
|
# задать высоту строк
|
|
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)
|
|
|
|
|
|
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 xrange(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
|
|
|
|
|
|
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], если в исходном листе
|
|
они были объединены, с сохранением исходных стилей форматирования.
|
|
"""
|
|
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:
|
|
if r1 < row_from or r1 > row_to:
|
|
continue
|
|
if c1 < col_from or c1 > col_to:
|
|
continue
|
|
|
|
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,
|
|
style)
|
|
|
|
|
|
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 xrange(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)
|
|
|
|
|
|
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):
|
|
"""
|
|
Размножить n_times раз строку из исходного листа, с сохранением стилей
|
|
форматирования.
|
|
"""
|
|
for offset in xrange(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)
|
|
|
|
# задать высоту строк
|
|
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)
|
|
|
|
|
|
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:
|
|
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])
|
|
|
|
|
|
def sum_src_heights(src_sheet, row_from, row_to):
|
|
"""Суммарная высота всех строк диапазона [row_from, row_to]
|
|
исходного листа.
|
|
"""
|
|
result = 0
|
|
for row in xrange(row_from, row_to+1):
|
|
src_rowinfo = src_sheet.rowinfo_map.get(row)
|
|
if src_rowinfo:
|
|
result += src_rowinfo.height
|
|
return result
|
|
|
|
|
|
def sum_dst_heights(dst_sheet, row_from, row_to):
|
|
"""Суммарная высота всех строк диапазона [row_from, row_to]
|
|
на новом листе.
|
|
"""
|
|
result = 0
|
|
for row in xrange(row_from, row_to+1):
|
|
result += dst_sheet.row(row).height
|
|
return result
|
|
|