From 92f13c6f20d44c2b168ed7ffbf5ee62ce70fe278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20H=C3=A4nninen?= Date: Thu, 14 Mar 2024 20:29:25 +0200 Subject: [PATCH 1/3] Improve `RPA.Excel.Application` library (#1158) --- docs/source/releasenotes.rst | 5 ++ packages/main/src/RPA/Excel/Application.py | 86 ++++++++++++++++++---- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/docs/source/releasenotes.rst b/docs/source/releasenotes.rst index 347e7f3760..40a7743c91 100644 --- a/docs/source/releasenotes.rst +++ b/docs/source/releasenotes.rst @@ -12,6 +12,11 @@ Latest versions `Upcoming release `_ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +- Library **RPA.Excel.Application** (:pr:`1158`): + + - Add keyword ``Remove hidden columns and rows`` from given range. + - Add keyword ``Merge range`` for merging cells in a range. + - Add keyword ``Unmerge range`` for unmerging cells in a range. `Released `_ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/packages/main/src/RPA/Excel/Application.py b/packages/main/src/RPA/Excel/Application.py index c5122573b2..858a4c8b45 100644 --- a/packages/main/src/RPA/Excel/Application.py +++ b/packages/main/src/RPA/Excel/Application.py @@ -420,7 +420,7 @@ def create_pivot_table( fields: List[PivotField], sort_field: PivotField = None, sort_direction: str = "descending", - data_range: str = None, + data_range: Any = None, pivot_name: str = "PivotTable1", collapse_rows: bool = True, show_grand_total: bool = True, @@ -482,9 +482,14 @@ def create_pivot_table( """ self.set_active_worksheet(source_worksheet) - excel_range = ( - self.worksheet.Range(data_range) if data_range else self.worksheet.UsedRange - ) + if data_range: + excel_range = ( + self.get_range(data_range) + if isinstance(data_range, str) + else data_range + ) + else: + excel_range = self.worksheet.UsedRange # Grab the pivot table source data pivot_cache = self.workbook.PivotCaches().Create( @@ -558,7 +563,7 @@ def to_sort_direction(sort_direction: str): def find( self, search_string: str, - search_range: str = None, + search_range: Any = None, max_results: int = None, search_order: SearchOrder = SearchOrder.ROWS, match_case: bool = False, @@ -612,9 +617,13 @@ def find( else constants.xlByColumns ) if search_range: - search_area = self._app.Range(search_range) + search_area = ( + self._app.Range(search_range) + if isinstance(search_range, str) + else search_range + ) else: - search_area = self.worksheet.Cells + search_area = self.worksheet.UsedRange # self.worksheet.Cells found = search_area.Find( What=search_string, MatchCase=match_case, SearchOrder=search_order ) @@ -635,18 +644,22 @@ def find( found = search_area.FindNext(found) return results - def create_table(self, table_name: str, table_range: str = None) -> None: + def create_table(self, table_name: str, table_range: Any = None) -> None: """Create a table in the current worksheet. :param table_name: name for the table :param table_range: source table range, if not given then the whole used range of `source_worksheet` will be used """ - excel_range = ( - self.worksheet.Range(table_range) - if table_range - else self.worksheet.UsedRange - ) + if table_range: + excel_range = ( + self.get_range(table_range) + if isinstance(table_range, str) + else table_range + ) + else: + excel_range = self.worksheet.UsedRange + self.worksheet.ListObjects.Add( SourceType=constants.xlSrcRange, Source=excel_range, @@ -823,3 +836,50 @@ def write_data_to_range( f"Total range column count {range_columns} " f"is different from data to write column count {row_columns}" ) + + def remove_hidden_columns_and_rows( + self, initial_range: Any, worksheet: Any = None + ) -> Any: + """Removes hidden columns and rows from a range and returns a new range. + + :param initial_range: range of cells to remove hidden columns and rows from + :param worksheet: set active worksheet before removing hidden columns and rows + :return: new range or initial range if no hidden cells found + """ + initial_range = ( + self.get_range(initial_range) + if isinstance(initial_range, str) + else initial_range + ) + if worksheet: + self.set_active_worksheet(worksheet) + try: + visible_range = initial_range.SpecialCells(constants.xlCellTypeVisible) + return visible_range + except Exception as e: # pylint: disable=broad-except + self.logger.error(f"No visible cells found or an error occurred:f{str(e)}") + return initial_range + + def unmerge_range(self, initial_range: Any) -> None: + """Unmerges a range of cells. + + :param initial_range: range of cells to unmerge + """ + initial_range = ( + self.get_range(initial_range) + if isinstance(initial_range, str) + else initial_range + ) + initial_range.UnMerge() + + def merge_range(self, initial_range: Any) -> None: + """Merges a range of cells. + + :param initial_range: range of cells to merge + """ + initial_range = ( + self.get_range(initial_range) + if isinstance(initial_range, str) + else initial_range + ) + initial_range.Merge() From f910f9779f905054773d897281adddfc0a23869d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20H=C3=A4nninen?= Date: Thu, 14 Mar 2024 20:39:46 +0200 Subject: [PATCH 2/3] Improve `RPA.Word.Application` selection usage (#1159) --- docs/source/releasenotes.rst | 6 ++++++ packages/main/src/RPA/Word/Application.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/docs/source/releasenotes.rst b/docs/source/releasenotes.rst index 40a7743c91..e55d266d82 100644 --- a/docs/source/releasenotes.rst +++ b/docs/source/releasenotes.rst @@ -12,6 +12,12 @@ Latest versions `Upcoming release `_ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +- Library **RPA.Word.Application** (:pr:`1159`): + + - Add keyword ``Select current paragraph`` for selecting the current paragraph. + - Add keyword ``Select paragraphs`` for selecting previous/next paragraphs. + - Add keyword ``Copy selection to clipboard`` for copying the selection to the clipboard. + - Library **RPA.Excel.Application** (:pr:`1158`): - Add keyword ``Remove hidden columns and rows`` from given range. diff --git a/packages/main/src/RPA/Word/Application.py b/packages/main/src/RPA/Word/Application.py index 6529f3c43f..5386286105 100644 --- a/packages/main/src/RPA/Word/Application.py +++ b/packages/main/src/RPA/Word/Application.py @@ -322,3 +322,24 @@ def write_text( self.app.Selection.TypeText(text) self.app.Selection.Select() + + def select_current_paragraph(self): + """Select text in current active paragraph.""" + self.app.Selection.Paragraphs(1).Range.Select() + + def copy_selection_to_clipboard(self): + """Copy current text selection to clipboard.""" + self.app.Selection.Copy() + + def select_paragraph(self, count: int = 1): + """Select paragraph(s) from current cursor position. + + Negative `count` moves cursor up number of paragraphs and + positive `count` moves cursor down number of paragraphs. + + :param count: number of paragraphs to select + """ + start = self.app.Selection.Range.Start + self.app.Selection.MoveDown(Unit=constants.wdParagraph, Count=count) + end = self.app.Selection.Range.End + self.app.Selection.SetRange(start, end) From d2fb72b1d3aaeea9732785ee83aac5222df98e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20H=C3=A4nninen?= Date: Thu, 14 Mar 2024 20:49:10 +0200 Subject: [PATCH 3/3] main. version 28.4.0 --- docs/source/releasenotes.rst | 9 ++++++--- packages/main/pyproject.toml | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/source/releasenotes.rst b/docs/source/releasenotes.rst index e55d266d82..02dedf2bd2 100644 --- a/docs/source/releasenotes.rst +++ b/docs/source/releasenotes.rst @@ -12,6 +12,12 @@ Latest versions `Upcoming release `_ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +`Released `_ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +28.4.0 - 14 Mar 2024 +-------------------- + - Library **RPA.Word.Application** (:pr:`1159`): - Add keyword ``Select current paragraph`` for selecting the current paragraph. @@ -24,9 +30,6 @@ Latest versions - Add keyword ``Merge range`` for merging cells in a range. - Add keyword ``Unmerge range`` for unmerging cells in a range. -`Released `_ -++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - 28.3.0 - 22 Feb 2024 -------------------- diff --git a/packages/main/pyproject.toml b/packages/main/pyproject.toml index a1e18bc5d0..5d888d3af7 100644 --- a/packages/main/pyproject.toml +++ b/packages/main/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "rpaframework" -version = "28.3.0" +version = "28.4.0" description = "A collection of tools and libraries for RPA" authors = ["RPA Framework "] license = "Apache-2.0"