Примеры макросов ACell-Листы

Вывод в диалоговое окно всех имен листов в документе ACELL с помощью макроса python


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def sheet_names():
   list_sheet_names = []

   doc = XSCRIPTCONTEXT.getDocument()
   sheet_names = doc.Sheets.ElementNames

   for name in sheet_names:
       list_sheet_names.append(name)

   msgbox(title="Названия листов в документе", msg="\n".join(map(str, list_sheet_names)))

Вывод в диалоговое окно количество листов в документе ACELL с помощью макроса python


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def number_of_sheets():
   doc = XSCRIPTCONTEXT.getDocument()
   count = doc.Sheets.Count
   msgbox(title="Колличество листов в файле", msg=f"Всего листов: {count}")


Перебрать все листы


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def loop_through_all_sheets():
   doc = XSCRIPTCONTEXT.getDocument()

   for sheet in doc.Sheets:
       msgbox(
               title="Все листы",
               msg=f"Лист: {sheet.Name}"
       )


Получить активный лист ACELL с помощью макроса python из текущего документа



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def get_active_sheet():
   doc = XSCRIPTCONTEXT.getDocument()
   sheet = doc.getCurrentController().getActiveSheet()
   msgbox(title="Активный/открытый лист", msg=sheet.Name)

Получить активный лист ACELL с помощью макроса python по индексу из текущего документа


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def get_active_sheet_by_index():
   doc = XSCRIPTCONTEXT.getDocument()
   try:
       sheet = doc.Sheets[2] # индекс массива - номер листа в документе
       msgbox(title="Активный/открытый лист по индексу в python", msg=sheet.Name)
   except IndexError as p:
       msgbox(title="Активный/открытый лист по индексу в python", msg="Неправильный индекс")

Получить активный лист ACELL с помощью макроса python по имени из текущего документа


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def get_active_sheet_by_index():
   doc = XSCRIPTCONTEXT.getDocument()
   try:
       sheet = doc.Sheets["name_list"]
       msgbox(title="Активный/открытый лист по имени в python", msg=sheet.Name)
   except KeyError as p:
       msgbox(title="Активный/открытый лист по имени в python", msg="Неправильное имя листа")


Переместиться на определенный лист в ACELL с помощью python по индексу в коде


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def focus_on_sheet_by_index():
   index = 4 #индекс массива - номер листа в документе
   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       msgbox(title="Фокус на лист по индексу", msg=f"Вы перешли на лист с индексом: {index}")
       doc.getCurrentController().setActiveSheet(sheet)
   except IndexError as p:
       msgbox(title="Фокус на лист по индексу", msg="Неправильный индекс!")


Изменить имя листа ACELL, с помощью python, видимое пользователю


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def change_sheet_name():
   index = 2
   new_name_sheet = "new_name_sheet"

   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       sheet.Name = new_name_sheet
       msgbox(title="Изменение имя листа", msg=f"Имя листа с индексом: {index} изменено на: {new_name_sheet}")
   except IndexError as p:
       msgbox(title="Изменение имя листа", msg="Неправильный индекс!")

Изменение имени листа ACELL с помощью python, видно только из кода


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def change_sheet_name_only_code():
   index = 2
   new_name_sheet = "new_name_sheet"

   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       sheet.CodeName = new_name_sheet
       msgbox(
               title="Изменение имя листа отображаемое только в коде",
               msg=f"Имя листа с индексом: {index} изменено на: {new_name_sheet}\nВнимание! Имя листа видно только из кода Python!"
       )
   except IndexError as p:
       msgbox(
               title="Изменение имя листа отображаемое только в коде",
               msg="Неправильный индекс!"
       )

Установить видимость/не видимость листа


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def sheet_visibility_true():
   index = 2
   title="Статус видимости листа"
   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       sheet.IsVisible = True
       msgbox(
               title=title,
               msg=f"Лист с индексом: {index} отображен!"
       )
   except IndexError as p:
       msgbox(
               title=title,
               msg="Неправильный индекс!"
       )

def sheet_visibility_false():
   index = 2
   title="Статус видимости листа"
   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       sheet.IsVisible = False
       msgbox(
               title=title,
               msg=f"Лист с индексом: {index} скрыт!"
       )
   except IndexError as p:
       msgbox(
               title=title,
               msg="Неправильный индекс!"
       )

Установка/удаление пароля


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def set_password():
   index = 2
   password = "your_password"

   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       sheet.protect(password)

       msgbox(
               title="Управление паролями",
               msg=f"Пароль для листа с индексом: {index} задан!"
       )
   except IndexError as p:
       msgbox(
               title="Ошибка",
               msg="Неправильный индекс!"
       )

def remove_password():
   index = 2
   password = "your_password"

   doc = XSCRIPTCONTEXT.getDocument()

   try:
       sheet = doc.Sheets[index]
       sheet.unprotect(password)

       msgbox(
               title="Управление паролями",
               msg=f"Пароль для листа с индексом: {index} удален!"
       )
   except IndexError as p:
       msgbox(
               title="Ошибка",
               msg="Неправильный индекс!"
       )


Проверьте, существует ли лист в книге


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def does_sheet_exist_book():
   find_name_sheet = "test"
   title = "Результат поиска!"

   doc = XSCRIPTCONTEXT.getDocument()

   if find_name_sheet in doc.Sheets:
       msgbox(
               title=title,
               msg=f"Лист: {find_name_sheet} существует!"
       )
   else:
       msgbox(
               title=title,
               msg=f"Лист: {find_name_sheet} не существует!"
       )

Вставьте пустой новый лист


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def insert_blank_new_sheet():
   index = 3
   name_new_sheet = "new"

   doc = XSCRIPTCONTEXT.getDocument()

   doc.Sheets.insertNewByName(name_new_sheet, index)
   msgbox(
           title="Вставьте пустой новый лист",
           msg=f"Лист: {name_new_sheet} вставлен"
   )

Переместить положение листа в книге


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def move_sheet_position_in_book():
   index = 3
   name_sheet = "new_test"

   title = "Результат"

   doc = XSCRIPTCONTEXT.getDocument()

   if name_sheet in doc.Sheets:

       doc.Sheets.moveByName(name_sheet, index)

       msgbox(
               title=title,
               msg=f"Лист с именем: {name_sheet} перенесен в положение с индексом: {index}"
       )
   else:
       msgbox(
               title=title,
               msg=f"Лист с именем: {name_sheet} не найден"
       )

Удалить лист из рабочей книги


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def move_sheet_position_in_book():
   name_sheet = "new_test"
   title = "Информация"

   doc = XSCRIPTCONTEXT.getDocument()
   count = doc.Sheets.Count

   if name_sheet in doc.Sheets:
       if count >= 2:
           doc.Sheets.removeByName(name_sheet)

           msgbox(
                   title=title,
                   msg=f"Лист: {name_sheet} заменен чистым листом!"
           )
       else:
           msgbox(
                   title=title,
                   msg=f"Листов мало, всего: {count}, добавьте еще!"
           )
   else:
       msgbox(
               title=title,
               msg=f"Лист: {name_sheet} не существует!"
       )

Заменить чистым листом


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def replace_with_blank_sheet():
   name_sheet = "new_test"
   title = "Информация"

   doc = XSCRIPTCONTEXT.getDocument()
   count = doc.Sheets.Count

   if name_sheet in doc.Sheets:
       if count >= 2:
           sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
           doc.Sheets.replaceByName(name_sheet, sheet)

           msgbox(
                   title=title,
                   msg=f"Лист: {name_sheet} - заменен чистым листом!"
           )
       else:
           msgbox(
                   title=title,
                   msg=f"Листов мало, всего: {count}, добавьте еще!"
           )
   else:
       msgbox(
               title=title,
               msg=f"Лист: {name_sheet} не существует!"
       )

Переименовать и копировать лист


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def rename_and_copy_sheet():
   source_name = "test_sheet"
   new_name = "new_test_sheet"
   position = 2

   title = "Информация"

   doc = XSCRIPTCONTEXT.getDocument()

   if source_name in doc.Sheets:
       doc.Sheets.copyByName(source_name, new_name, position)

       msgbox(
               title=title,
               msg=f"Лист: {source_name} копирован и переименован в: {new_name}"
       )
   else:
       msgbox(
               title=title,
               msg=f"Лист: {source_name} не существует!"
       )

Копирование/импорт листа с названием "test_sheet" в другой документ



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def create_instance(name, context=False):
   CTX = uno.getComponentContext()
   SM = CTX.getServiceManager()

   if context:
       instance = SM.createInstanceWithContext(name, CTX)
   else:
       instance = SM.createInstance(name)
   return instance

def copying_sheet_another_workbook():
   title = "Информация"
   name_sheet = "test_sheet"

   doc = XSCRIPTCONTEXT.getDocument()

   if name_sheet in doc.Sheets:
       path = "private:factory/scalc"
       desktop = create_instance("com.sun.star.frame.Desktop", context=True)

       source = desktop.loadComponentFromURL(path, "_default", 0, ())
       source.Sheets.importSheet(doc, name_sheet, 0)

       msgbox(
               title=title,
               msg=f"Лист: {name_sheet} скопирован в новый документ!"
       )
   else:
       msgbox(
               title=title,
               msg=f"Лист: {name_sheet} не существует!"
       )

Раскрасить лист с названием test_sheet


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uno
from com.sun.star.awt.MessageBoxType import INFOBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_YES_NO_CANCEL, DEFAULT_BUTTON_YES

def msgbox(msg=None, title=None):
   if msg is None:
       msg = "HELLO WORLD"
   else:
       msg = msg

   if title is None:
       title = "ТЕСТОВОЕ ДИАЛОГОВОЕ ОКНО"
   else:
       title = title

   ctx = uno.getComponentContext()
   smgr = ctx.getServiceManager()
   tk = smgr.createInstance("com.sun.star.awt.Toolkit")
   dtp = smgr.createInstance("com.sun.star.frame.Desktop")
   frame = dtp.getCurrentFrame()
   win = frame.getComponentWindow()
   mtyp = INFOBOX
   btn = BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES
   mb = tk.createMessageBox(win, mtyp, btn, title, msg)

   return mb.execute()

def get_color(red, green, blue):
   return (red << 16) + (green << 8) + blue

def сolorize_tab_sheet():
   title = "Информация"
   name_sheet = "test_sheet"

   doc = XSCRIPTCONTEXT.getDocument()

   if name_sheet in doc.Sheets:
       sheet = doc.Sheets[name_sheet]
       sheet.TabColor = get_color(255, 0, 0)

       msgbox(
               title=title,
               msg=f"Лист: {name_sheet} - перекрашен!"
       )
   else:
       msgbox(
               title=title,
               msg=f"Лист: {name_sheet} не существует!"
       )