diff --git a/src/pptxlib/app.py b/src/pptxlib/app.py index e63ea80..9d502a4 100644 --- a/src/pptxlib/app.py +++ b/src/pptxlib/app.py @@ -178,3 +178,33 @@ def active(self): # def add_table(self, *args, **kwargs): # return self.slide.shapes.add_table(*args, **kwargs) +# """ +# CustomLayoutに関連するモジュール +# """ + + +# def copy_layout(slide, name=None, replace=True): +# """指定するスライドのCustomLayoutをコピーして返す. + +# Parameters +# ---------- +# slide : xlviews.powerpoint.main.Slide +# スライドオブジェクト +# name : str, optional +# CustomLayoutの名前 +# replace : bool, optional +# スライドのCustomLayoutをコピーしたものに +# 置き換えるか + +# Returns +# ------- +# layout +# """ +# layouts = slide.parent.api.SlideMaster.CustomLayouts +# slide.api.CustomLayout.Copy() +# layout = layouts.Paste() +# if name: +# layout.Name = name +# if replace: +# slide.api.CustomLayout = layout +# return layout diff --git a/src/pptxlib/layout.py b/src/pptxlib/layout.py deleted file mode 100644 index 04734e4..0000000 --- a/src/pptxlib/layout.py +++ /dev/null @@ -1,30 +0,0 @@ -# """ -# CustomLayoutに関連するモジュール -# """ - - -# def copy_layout(slide, name=None, replace=True): -# """指定するスライドのCustomLayoutをコピーして返す. - -# Parameters -# ---------- -# slide : xlviews.powerpoint.main.Slide -# スライドオブジェクト -# name : str, optional -# CustomLayoutの名前 -# replace : bool, optional -# スライドのCustomLayoutをコピーしたものに -# 置き換えるか - -# Returns -# ------- -# layout -# """ -# layouts = slide.parent.api.SlideMaster.CustomLayouts -# slide.api.CustomLayout.Copy() -# layout = layouts.Paste() -# if name: -# layout.Name = name -# if replace: -# slide.api.CustomLayout = layout -# return layout diff --git a/src/pptxlib/style.py b/src/pptxlib/style.py deleted file mode 100644 index 461653d..0000000 --- a/src/pptxlib/style.py +++ /dev/null @@ -1,72 +0,0 @@ -# from win32com.client import constants - -# from xlviews.utils import rgb - - -# def set_border_cell(cell, pos, width=1, color=0, line_style='-', visible=True): -# pos = getattr(constants, 'ppBorder' + pos[0].upper() + pos[1:]) -# border = cell.Borders(pos) -# border.Visible = visible -# if not visible: -# return -# border.Weight = width -# border.ForeColor.RGB = color -# if line_style == '-.': -# border.DashStyle = constants.msoLineDash - - -# def set_border(table, start, end, edge_width=2, inside_width=1, edge_color=0, -# inside_color=rgb(140, 140, 140), edge_line_style='-', -# inside_line_style='-'): - -# if inside_width: -# kwargs = dict(width=inside_width, color=inside_color, -# line_style=inside_line_style) -# for row in range(start[0], end[0] + 1): -# for column in range(start[1], end[1]): -# cell = table.Cell(row, column) -# set_border_cell(cell, 'right', **kwargs) -# for column in range(start[1], end[1] + 1): -# for row in range(start[0], end[0]): -# cell = table.Cell(row, column) -# set_border_cell(cell, 'bottom', **kwargs) - -# if edge_width: -# kwargs = dict(width=edge_width, color=edge_color, -# line_style=edge_line_style) -# for row in range(start[0], end[0] + 1): -# cell = table.Cell(row, start[1]) -# set_border_cell(cell, 'left', **kwargs) -# cell = table.Cell(row, end[1]) -# set_border_cell(cell, 'right', **kwargs) -# for column in range(start[1], end[1] + 1): -# cell = table.Cell(start[0], column) -# set_border_cell(cell, 'top', **kwargs) -# cell = table.Cell(end[0], column) -# set_border_cell(cell, 'bottom', **kwargs) - - -# def set_fill(table, start, end, fill): -# for row in range(start[0], end[0] + 1): -# for column in range(start[1], end[1] + 1): -# cell = table.Cell(row, column) -# cell.Shape.Fill.ForeColor.RGB = fill - - -# def set_font(table, start, end, size=10): -# for row in range(start[0], end[0] + 1): -# for column in range(start[1], end[1] + 1): -# cell = table.cell(row, column) -# print(cell) - - -# def main(): -# import xlviews.powerpoint.table - -# table = xlviews.powerpoint.table.main() -# set_border(table, (4, 1), (7, 2)) -# return table - - -# if __name__ == '__main__': -# table = main() diff --git a/src/pptxlib/table.py b/src/pptxlib/table.py deleted file mode 100644 index d37d477..0000000 --- a/src/pptxlib/table.py +++ /dev/null @@ -1,145 +0,0 @@ -# from itertools import product - -# import numpy as np -# import pandas as pd -# from win32com.client import constants - -# from xlviews.format import CellContainer, ContainerStyle -# from xlviews.powerpoint.style import set_border, set_border_cell, set_fill - - -# def empty_frame(index, columns, columns_name, index_name): - -# def multiindex(index): -# if isinstance(index[0], str): -# return [[i] for i in index] -# else: -# return list(np.array(index).T) - -# # index = ['a', 'b'] -# # columns = [['x', 'y', 'a'], ['x', 'y', 'b']] -# # index = multiindex(index) -# # columns = multiindex(columns) -# # index -# # -# # empty = np.zeros((len(index[0]), len(columns[0])), dtype=str) -# # empty -# # -# # df = pd.DataFrame() -# # df.columns = list(np.array(columns).T.T) -# # -# # df -# # -# # df.columns = columns -# # df.index = np.array(index) -# # df - - -# def create_table(obj, df, left=100, top=100, width=100, height=100, -# merge=True, index=True, index_name=True, columns_name=True, -# border_style=None, font_size=10, preclean=True): -# """ -# マージ機能付きの表を作成する. -# """ -# # nrows, ncols = df.shape -# # columns_level = len(df.columns.levels) -# # index_level = len(df.index.levels) -# # blank_row = 1 if df.columns.names else 0 -# cell_container = CellContainer(df, merge_cells=merge, index=index, -# index_name=index_name, -# columns_name=columns_name) -# shape = obj.AddTable(cell_container.nrows, cell_container.ncols, -# left, top, width, height) -# table = shape.Table - -# from xlviews.powerpoint.main import Shape -# parent = obj.parent if hasattr(obj, 'parent') else None -# shape = Shape(shape, parent=parent) - -# for cell in shape.table.cells(): -# cell.shape.size = font_size - -# if preclean: -# clear(table) - -# # column_width = {} - -# for cell in cell_container.cells: -# table_cell = table.Cell(cell.row, cell.col) -# if hasattr(table_cell, 'Shape'): -# text_frame = table_cell.Shape.TextFrame -# text_range = text_frame.TextRange -# text_range.Text = str(cell.val) -# # text_range.Font.Size = font_size -# text_range.Font.Bold = ContainerStyle.style['bold'][cell.type] -# text_range.ParagraphFormat.Alignment = constants.ppAlignCenter -# text_frame.VerticalAnchor = constants.msoAnchorMiddle - -# if merge and cell.merge_row is not None and cell.merge_col is not None: -# merge_cell = table.Cell(cell.merge_row, cell.merge_col) -# table_cell.Merge(merge_cell) - -# for container in cell_container.containers: -# start = (container.row_min, container.col_min) -# end = (container.row_max, container.col_max) -# border_style = border_style or {} -# set_border(table, start, end, **border_style) -# fill = container.style['fill'] -# set_fill(table, start, end, fill) - -# shape.table.minimize_height() - -# return shape - - -# def clear(table): -# table.FirstCol = False -# table.FirstRow = False -# table.HorizBanding = False - -# nrows = len(table.Columns(1).Cells) -# ncols = len(table.Rows(1).Cells) -# for row, column in product(range(nrows), range(ncols)): -# cell = table.Cell(row + 1, column + 1) -# if row == 0: -# set_border_cell(cell, 'top', visible=False) -# if column == 0: -# set_border_cell(cell, 'left', visible=False) -# set_border_cell(cell, 'right', visible=False) -# set_border_cell(cell, 'bottom', visible=False) -# cell.Shape.Fill.Visible = False - - -# def _prepare_frame(): -# df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) -# df.index = [['a', 'a', 'a', 'a'], ['x', 'y', 'y', 'z']] -# df.columns = [['A', 'B', 'B'], ['s', 't', 't']] -# df.index.names = ['i1', 'i2'] -# df.columns.names = ['c1', 'c2'] -# return df - - -# def main(): -# import xlviews as xv -# pp = xv.PowerPoint() - -# df = _prepare_frame() - -# for table in pp.tables: -# table.parent.api.Delete() - -# shape = create_table(pp.slide.shapes, df, -# left=200, top=100, width=300, height=200, -# columns_name=True, index_name=False) - -# return shape.table - - -# if __name__ == '__main__': -# table = main() - -# text_frame = table.cell(1, 4).shape.api.TextFrame -# text_range = table.cell(1, 4).shape.api.TextFrame.TextRange -# text_range.ParagraphFormat.Alignment = constants.ppAlignCenter -# # text_range.BoundWidth -# text_frame.VerticalAnchor = constants.msoAnchorMiddle diff --git a/src/pptxlib/tables.py b/src/pptxlib/tables.py index a97dcf6..56a2d94 100644 --- a/src/pptxlib/tables.py +++ b/src/pptxlib/tables.py @@ -1,7 +1,9 @@ from __future__ import annotations from dataclasses import dataclass -from typing import TYPE_CHECKING, ClassVar +from typing import TYPE_CHECKING, ClassVar, Literal + +from win32com.client import constants from pptxlib.core import Collection, Element from pptxlib.shapes import Shape @@ -75,6 +77,24 @@ def cell(self, row: int, column: int | None = None) -> Cell: return Cell(self.api.Cell(row, column), self) +# def clear(table): +# table.FirstCol = False +# table.FirstRow = False +# table.HorizBanding = False + +# nrows = len(table.Columns(1).Cells) +# ncols = len(table.Rows(1).Cells) +# for row, column in product(range(nrows), range(ncols)): +# cell = table.Cell(row + 1, column + 1) +# if row == 0: +# set_border_cell(cell, 'top', visible=False) +# if column == 0: +# set_border_cell(cell, 'left', visible=False) +# set_border_cell(cell, 'right', visible=False) +# set_border_cell(cell, 'bottom', visible=False) +# cell.Shape.Fill.Visible = False + + @dataclass(repr=False) class Tables(Collection[Table]): parent: Slide @@ -210,6 +230,90 @@ def value(self): def value(self, value): self.text = value + def set_border( + self, + pos: str, + width: float = 1, + color: int | str | tuple[int, int, int] = 0, + line_style: Literal["-", "--"] = "-", + *, + visible: bool = True, + ): + pos = getattr(constants, "ppBorder" + pos[0].upper() + pos[1:]) + border = self.api.Borders(pos) + border.Visible = visible + + if not visible: + return + + border.Weight = width + border.ForeColor.RGB = color + + if line_style == "--": + border.DashStyle = constants.msoLineDash + + +# from win32com.client import constants + +# from xlviews.utils import rgb + + +# def set_border(table, start, end, edge_width=2, inside_width=1, edge_color=0, +# inside_color=rgb(140, 140, 140), edge_line_style='-', +# inside_line_style='-'): + +# if inside_width: +# kwargs = dict(width=inside_width, color=inside_color, +# line_style=inside_line_style) +# for row in range(start[0], end[0] + 1): +# for column in range(start[1], end[1]): +# cell = table.Cell(row, column) +# set_border_cell(cell, 'right', **kwargs) +# for column in range(start[1], end[1] + 1): +# for row in range(start[0], end[0]): +# cell = table.Cell(row, column) +# set_border_cell(cell, 'bottom', **kwargs) + +# if edge_width: +# kwargs = dict(width=edge_width, color=edge_color, +# line_style=edge_line_style) +# for row in range(start[0], end[0] + 1): +# cell = table.Cell(row, start[1]) +# set_border_cell(cell, 'left', **kwargs) +# cell = table.Cell(row, end[1]) +# set_border_cell(cell, 'right', **kwargs) +# for column in range(start[1], end[1] + 1): +# cell = table.Cell(start[0], column) +# set_border_cell(cell, 'top', **kwargs) +# cell = table.Cell(end[0], column) +# set_border_cell(cell, 'bottom', **kwargs) + + +# def set_fill(table, start, end, fill): +# for row in range(start[0], end[0] + 1): +# for column in range(start[1], end[1] + 1): +# cell = table.Cell(row, column) +# cell.Shape.Fill.ForeColor.RGB = fill + + +# def set_font(table, start, end, size=10): +# for row in range(start[0], end[0] + 1): +# for column in range(start[1], end[1] + 1): +# cell = table.cell(row, column) +# print(cell) + + +# def main(): +# import xlviews.powerpoint.table + +# table = xlviews.powerpoint.table.main() +# set_border(table, (4, 1), (7, 2)) +# return table + + +# if __name__ == '__main__': +# table = main() + # # # def align(self, shape, pos=(0, 0)): # # # x, y = pos diff --git a/tests/test_tables.py b/tests/test_tables.py index c57c1b1..82e4aa3 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -129,3 +129,18 @@ def test_repr_tables(tables: Tables): def test_repr_table(table: Table): assert repr(table) == "