Add autoglm-os-9b-v (#344)

* update for autoglm-v

* Update run_autoglm.py

---------

Co-authored-by: hanyullai <hanyullai@outlook.com>
This commit is contained in:
Yanxiao Zhao
2025-09-24 19:43:28 +08:00
committed by GitHub
parent f59cf00cae
commit a4f8fe2f00
23 changed files with 8425 additions and 2 deletions

View File

@@ -0,0 +1,3 @@
from .func import generate_func
__all__ = ["generate_func"]

View File

@@ -0,0 +1,236 @@
[
{
"type": "function",
"function": {
"name": "CodeTools.launch_vscode",
"description": "Launch VS Code with specified path",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "File path or directory to open"
}
},
"required": ["path"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.compare_files",
"description": "Compare two files in VS Code",
"parameters": {
"type": "object",
"properties": {
"file1": {
"type": "string",
"description": "First file path"
},
"file2": {
"type": "string",
"description": "Second file path"
}
},
"required": ["file1", "file2"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.add_folder",
"description": "Add folder to active VS Code window",
"parameters": {
"type": "object",
"properties": {
"folder": {
"type": "string",
"description": "Folder path to add"
}
},
"required": ["folder"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.goto_file",
"description": "Open file at specific position",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "File path to open"
},
"line": {
"type": "integer",
"description": "Line number",
"default": 1
},
"character": {
"type": "integer",
"description": "Character position",
"default": 1
}
},
"required": ["file_path"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.perform_merge",
"description": "Perform three-way merge",
"parameters": {
"type": "object",
"properties": {
"path1": {
"type": "string",
"description": "First version file path"
},
"path2": {
"type": "string",
"description": "Second version file path"
},
"base": {
"type": "string",
"description": "Base version file path"
},
"result": {
"type": "string",
"description": "Output file path"
}
},
"required": ["path1", "path2", "base", "result"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.remove_folder",
"description": "Remove folder from active VS Code window",
"parameters": {
"type": "object",
"properties": {
"folder": {
"type": "string",
"description": "Folder path to remove"
}
},
"required": ["folder"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.install_extension",
"description": "Install or update VS Code extension",
"parameters": {
"type": "object",
"properties": {
"extension_id": {
"type": "string",
"description": "Extension identifier"
},
"pre_release": {
"type": "boolean",
"description": "Install pre-release version",
"default": false
}
},
"required": ["extension_id"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.uninstall_extension",
"description": "Uninstall VS Code extension",
"parameters": {
"type": "object",
"properties": {
"extension_id": {
"type": "string",
"description": "Extension identifier"
}
},
"required": ["extension_id"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.list_extensions",
"description": "List installed extensions",
"parameters": {
"type": "object",
"properties": {
"show_versions": {
"type": "boolean",
"description": "Show extension versions",
"default": false
},
"category": {
"type": "string",
"description": "Filter by category"
}
}
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.update_extensions",
"description": "Update all extensions to latest version",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.disable_extension",
"description": "Disable extension for next VS Code instance",
"parameters": {
"type": "object",
"properties": {
"extension_id": {
"type": "string",
"description": "Extension identifier"
}
},
"required": ["extension_id"]
}
}
},
{
"type": "function",
"function": {
"name": "CodeTools.toggle_sync",
"description": "Toggle VS Code synchronization",
"parameters": {
"type": "object",
"properties": {
"state": {
"type": "string",
"description": "Sync state",
"enum": ["on", "off"]
}
},
"required": ["state"]
}
}
}
]

View File

@@ -0,0 +1,117 @@
def generate_func(json_data):
# 收集所有类名和它们的函数
class_funcs = {}
no_class_funcs = []
for item in json_data:
if item["type"] == "function":
func = item["function"]
func_parts = func["name"].split(".")
if len(func_parts) == 2:
class_name, func_name = func_parts
if class_name not in class_funcs:
class_funcs[class_name] = []
class_funcs[class_name].append(item)
else:
no_class_funcs.append(item)
code = ""
# 生成有类的函数
for class_name, funcs in class_funcs.items():
code += f"class {class_name}:\n"
for item in funcs:
func = item["function"]
func_name = func["name"].split(".")[-1]
description = func["description"]
params = func["parameters"]["properties"]
required = func["parameters"].get("required", [])
# 构建参数列表
param_list = ["cls"]
# 首先添加必需参数
for param_name in required:
param_list.append(f"{param_name}")
# 然后添加可选参数
for param_name in params:
if param_name not in required:
param_list.append(f"{param_name}") # 可选参数默认值设为None
# 构建函数定义
func_def = f" def {func_name}({', '.join(param_list)}):\n"
# 构建文档字符串
docstring = f' """\n {description}\n\n Args:\n'
if len(param_list) == 1: # 只有cls参数
docstring += " None\n"
else:
# 首先记录必需参数
for param_name in required:
param_type = params[param_name]["type"]
param_desc = params[param_name].get("description", "")
docstring += f" {param_name} ({param_type}): {param_desc}\n"
# 然后记录可选参数
for param_name in params:
if param_name not in required:
param_type = params[param_name]["type"]
param_desc = params[param_name].get("description", "")
docstring += f" {param_name} ({param_type}, optional): {param_desc}\n"
docstring += ' """\n'
code += func_def + docstring + "\n"
code += "\n"
# 生成没有类的函数
for item in no_class_funcs:
func = item["function"]
func_name = func["name"]
description = func["description"]
params = func["parameters"]["properties"]
required = func["parameters"].get("required", [])
# 构建参数列表
param_list = []
# 首先添加必需参数
for param_name in required:
param_list.append(f"{param_name}")
# 然后添加可选参数
for param_name in params:
if param_name not in required:
param_list.append(f"{param_name}")
# 构建函数定义
func_def = f"def {func_name}({', '.join(param_list)}):\n"
# 构建文档字符串
docstring = f' """\n {description}\n\n Args:\n'
if not param_list:
docstring += " None\n"
else:
# 首先记录必需参数
for param_name in required:
param_type = params[param_name]["type"]
param_desc = params[param_name].get("description", "")
docstring += f" {param_name} ({param_type}): {param_desc}\n"
# 然后记录可选参数
for param_name in params:
if param_name not in required:
param_type = params[param_name]["type"]
param_desc = params[param_name].get("description", "")
docstring += f" {param_name} ({param_type}, optional): {param_desc}\n"
docstring += ' """\n'
code += func_def + docstring + "\n"
return code.strip()
if __name__ == "__main__":
import json
with open("libreoffice_calc.json", "r") as f:
json_data = json.load(f)
print(generate_func(json_data))

View File

@@ -0,0 +1,134 @@
[
{
"type": "function",
"function": {
"name": "BrowserTools.open_profile_settings",
"description": "Opens profile settings page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.open_password_settings",
"description": "Opens password/autofill settings page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.open_privacy_settings",
"description": "Opens privacy settings page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.open_appearance_settings",
"description": "Opens appearance settings page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.open_search_engine_settings",
"description": "Opens search engine settings page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.bring_back_last_tab",
"description": "Restores last-closed tab (Ctrl+Shift+T).",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.print",
"description": "Opens print dialog (Ctrl+P).",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.delete_browsing_data",
"description": "Opens clear browsing data dialog (Ctrl+Shift+Del).",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.open_extensions",
"description": "Opens extensions management page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.bookmark_page",
"description": "Bookmarks current page (Ctrl+D).",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "BrowserTools.open_bookmarks",
"description": "Opens bookmarks page.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
}
]

View File

@@ -0,0 +1,634 @@
[
{
"type": "function",
"function": {
"name": "CalcTools.get_workbook_info",
"description": "Get workbook info: file path, name, sheets, and active sheet",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.save",
"description": "Save workbook to current location",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.get_column_data",
"description": "Get all data from specified column",
"parameters": {
"type": "object",
"properties": {
"column_name": {
"type": "string",
"description": "Column name (e.g. 'A', 'B')"
}
},
"required": [
"column_name"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.switch_active_sheet",
"description": "Switch to sheet (creates if not exists)",
"parameters": {
"type": "object",
"properties": {
"sheet_name": {
"type": "string",
"description": "Sheet name"
}
},
"required": [
"sheet_name"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.set_column_values",
"description": "Set values to column (values only, not formulas)",
"parameters": {
"type": "object",
"properties": {
"column_name": {
"type": "string",
"description": "Column name (e.g. 'A', 'B')"
},
"data": {
"type": "array",
"description": "Values to write"
},
"start_index": {
"type": "integer",
"description": "First row index (default: 2)"
}
},
"required": [
"column_name",
"data"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.highlight_range",
"description": "Highlight range with color",
"parameters": {
"type": "object",
"properties": {
"range_str": {
"type": "string",
"description": "Range (e.g. 'A1:B10')"
},
"color": {
"type": "integer",
"description": "Color value (default: 0xFF0000)"
}
},
"required": [
"range_str"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.transpose_range",
"description": "Transpose range and paste to target cell",
"parameters": {
"type": "object",
"properties": {
"source_range": {
"type": "string",
"description": "Source range (e.g. 'A1:B10')"
},
"target_cell": {
"type": "string",
"description": "Target cell (e.g. 'A1')"
}
},
"required": [
"source_range",
"target_cell"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.export_to_csv",
"description": "Export to CSV with same path/name",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.sort_column",
"description": "Sort column data",
"parameters": {
"type": "object",
"properties": {
"column_name": {
"type": "string",
"description": "Column name (e.g. 'A', 'B')"
},
"ascending": {
"type": "boolean",
"description": "Sort ascending (default: true)"
},
"start_index": {
"type": "integer",
"description": "First row index (default: 2)"
}
},
"required": [
"column_name"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.set_validation_list",
"description": "Set validation list for column",
"parameters": {
"type": "object",
"properties": {
"column_name": {
"type": "string",
"description": "Column name (e.g. 'A', 'B')"
},
"values": {
"type": "array",
"description": "Validation values"
}
},
"required": [
"column_name",
"values"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.hide_row_data",
"description": "Hide rows containing value",
"parameters": {
"type": "object",
"properties": {
"value": {
"type": "string",
"description": "Value to hide (default: 'N/A')"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.reorder_columns",
"description": "Reorder columns by specified order",
"parameters": {
"type": "object",
"properties": {
"column_order": {
"type": "array",
"description": "Column names in desired order (e.g. ['A', 'B', 'C'])"
}
},
"required": [
"column_order"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.create_pivot_table",
"description": "Create pivot table from source sheet",
"parameters": {
"type": "object",
"properties": {
"source_sheet": {
"type": "string",
"description": "Source sheet name"
},
"table_name": {
"type": "string",
"description": "Pivot table name"
},
"row_fields": {
"type": "array",
"description": "Row labels (e.g. ['A', 'B'])"
},
"col_fields": {
"type": "array",
"description": "Column labels (e.g. ['A', 'B'])"
},
"value_fields": {
"type": "array",
"description": "Value fields (e.g. ['A', 'B'])"
},
"aggregation_function": {
"type": "string",
"description": "Aggregation function (sum, count, average, min, max)"
},
"target_cell": {
"type": "string",
"description": "Target cell (default: 'A1')"
}
},
"required": [
"source_sheet",
"table_name",
"value_fields"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.merge_cells",
"description": "Merge cells in range",
"parameters": {
"type": "object",
"properties": {
"range_str": {
"type": "string",
"description": "Cell range (e.g. 'A1:B10')"
}
},
"required": [
"range_str"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.set_cell_value",
"description": "Set cell value",
"parameters": {
"type": "object",
"properties": {
"cell": {
"type": "string",
"description": "Cell reference (e.g. 'A1')"
},
"value": {
"type": "string",
"description": "Cell value"
}
},
"required": [
"cell",
"value"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.format_range",
"description": "Apply formatting to range",
"parameters": {
"type": "object",
"properties": {
"range_str": {
"type": "string",
"description": "Range (e.g. 'A1:B10')"
},
"background_color": {
"type": "string",
"description": "Background color (e.g. '#0000ff')"
},
"font_color": {
"type": "string",
"description": "Font color (e.g. '#ffffff')"
},
"bold": {
"type": "boolean",
"description": "Bold text"
},
"alignment": {
"type": "string",
"description": "Text alignment (left, center, right)"
}
},
"required": [
"range_str"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.create_chart",
"description": "Create chart from data range",
"parameters": {
"type": "object",
"properties": {
"chart_type": {
"type": "string",
"description": "Chart type (bar, column, line, pie, scatter, area)"
},
"data_range": {
"type": "string",
"description": "Data range (e.g. 'A1:B10')"
},
"title": {
"type": "string",
"description": "Chart title"
},
"x_axis_title": {
"type": "string",
"description": "X axis title"
},
"y_axis_title": {
"type": "string",
"description": "Y axis title"
}
},
"required": [
"chart_type",
"data_range"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.freeze_panes",
"description": "Freeze rows/columns",
"parameters": {
"type": "object",
"properties": {
"rows": {
"type": "integer",
"description": "Rows to freeze from top"
},
"columns": {
"type": "integer",
"description": "Columns to freeze from left"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.rename_sheet",
"description": "Rename worksheet",
"parameters": {
"type": "object",
"properties": {
"old_name": {
"type": "string",
"description": "Current sheet name"
},
"new_name": {
"type": "string",
"description": "New sheet name"
}
},
"required": [
"old_name",
"new_name"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.copy_sheet",
"description": "Copy worksheet",
"parameters": {
"type": "object",
"properties": {
"source_sheet": {
"type": "string",
"description": "Source sheet name"
},
"new_sheet_name": {
"type": "string",
"description": "New sheet name (optional)"
}
},
"required": [
"source_sheet"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.reorder_sheets",
"description": "Change sheet order",
"parameters": {
"type": "object",
"properties": {
"sheet_name": {
"type": "string",
"description": "Sheet to move"
},
"position": {
"type": "integer",
"description": "New position (0-based)"
}
},
"required": [
"sheet_name",
"position"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.set_chart_legend_position",
"description": "Set chart legend position",
"parameters": {
"type": "object",
"properties": {
"position": {
"type": "string",
"description": "Legend position (top, bottom, left, right, none)"
}
},
"required": [
"position"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.set_number_format",
"description": "Apply number format to range",
"parameters": {
"type": "object",
"properties": {
"range_str": {
"type": "string",
"description": "Range (e.g. 'A1:B10')"
},
"format_type": {
"type": "string",
"description": "Format type (general, number, currency, accounting, date, time, percentage, fraction, scientific, text)"
},
"decimal_places": {
"type": "integer",
"description": "Decimal places (optional)"
}
},
"required": [
"range_str",
"format_type"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.adjust_column_width",
"description": "Adjust column width",
"parameters": {
"type": "object",
"properties": {
"columns": {
"type": "string",
"description": "Column range (e.g. 'A:C')"
},
"width": {
"type": "number",
"description": "Width in characters"
},
"autofit": {
"type": "boolean",
"description": "Autofit to content"
}
},
"required": [
"columns"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.adjust_row_height",
"description": "Adjust row height",
"parameters": {
"type": "object",
"properties": {
"rows": {
"type": "string",
"description": "Row range (e.g. '1:10')"
},
"height": {
"type": "number",
"description": "Height in points"
},
"autofit": {
"type": "boolean",
"description": "Autofit to content"
}
},
"required": [
"rows"
]
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.export_to_pdf",
"description": "Export to PDF",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "PDF save path (default: same as original)"
},
"sheets": {
"type": "array",
"description": "Sheets to include (default: all)"
},
"open_after_export": {
"type": "boolean",
"description": "Open PDF after export (default: false)"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "CalcTools.set_zoom_level",
"description": "Set worksheet zoom level",
"parameters": {
"type": "object",
"properties": {
"zoom_percentage": {
"type": "integer",
"description": "Zoom percentage (10-400)"
}
},
"required": [
"zoom_percentage"
]
}
}
}
]

View File

@@ -0,0 +1,559 @@
[
{
"type": "function",
"function": {
"name": "ImpressTools.save",
"description": "Save current presentation",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.go_to_slide",
"description": "Navigate to specific slide",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
}
},
"required": ["slide_index"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.get_slide_count",
"description": "Get total slide count",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.duplicate_slide",
"description": "Duplicate slide and place at end",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index to duplicate (1-based)"
}
},
"required": ["slide_index"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_slide_font",
"description": "Set font for all text in slide",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"font_name": {
"type": "string",
"description": "Font name (e.g., 'Arial', 'Times New Roman')"
}
},
"required": ["slide_index", "font_name"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.write_text",
"description": "Write text to textbox",
"parameters": {
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "Text content"
},
"page_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
},
"bold": {
"type": "boolean",
"description": "Bold text (default: false)"
},
"italic": {
"type": "boolean",
"description": "Italic text (default: false)"
},
"size": {
"type": "integer",
"description": "Font size"
},
"append": {
"type": "boolean",
"description": "Append to existing text (default: false)"
}
},
"required": ["content", "page_index", "box_index"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_style",
"description": "Set text style for textbox",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
},
"bold": {
"type": "boolean",
"description": "Bold text"
},
"italic": {
"type": "boolean",
"description": "Italic text"
},
"underline": {
"type": "boolean",
"description": "Underline text"
}
},
"required": ["slide_index", "box_index"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.configure_auto_save",
"description": "Configure auto-save settings",
"parameters": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean",
"description": "Enable auto-save"
},
"interval_minutes": {
"type": "number",
"description": "Auto-save interval in minutes (min: 1)"
}
},
"required": ["enabled", "interval_minutes"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_background_color",
"description": "Set textbox background color",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
},
"color": {
"type": "string",
"description": "Color name or hex code"
}
},
"required": ["slide_index", "box_index", "color"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_text_color",
"description": "Set text color for textbox",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
},
"color": {
"type": "string",
"description": "Color name or hex code"
}
},
"required": ["slide_index", "box_index", "color"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.delete_content",
"description": "Delete textbox from slide",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
}
},
"required": ["slide_index", "box_index"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_slide_orientation",
"description": "Set slide orientation",
"parameters": {
"type": "object",
"properties": {
"orientation": {
"type": "string",
"description": "Slide orientation",
"enum": ["portrait", "landscape"]
}
},
"required": ["orientation"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.position_box",
"description": "Position textbox or image on slide",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Box index (0-based)"
},
"position": {
"type": "string",
"description": "Position on slide",
"enum": ["left", "right", "center", "top", "bottom", "top-left", "top-right", "bottom-left", "bottom-right"]
}
},
"required": ["slide_index", "box_index", "position"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.insert_file",
"description": "Insert video or audio file",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "File path"
},
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"position": {
"type": "object",
"description": "Position coordinates",
"properties": {
"x": {
"type": "number",
"description": "X position (% of slide width)"
},
"y": {
"type": "number",
"description": "Y position (% of slide height)"
}
}
},
"size": {
"type": "object",
"description": "Size dimensions",
"properties": {
"width": {
"type": "number",
"description": "Width (% of slide width)"
},
"height": {
"type": "number",
"description": "Height (% of slide height)"
}
}
},
"autoplay": {
"type": "boolean",
"description": "Auto-play media"
}
},
"required": ["file_path"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_slide_background",
"description": "Set slide background color or image",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based). If not provided, applies to all slides"
},
"color": {
"type": "string",
"description": "Background color"
},
"image_path": {
"type": "string",
"description": "Background image path (overrides color)"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.save_as",
"description": "Save document to specified location",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "File save path with filename and extension"
},
"overwrite": {
"type": "boolean",
"description": "Overwrite existing file (default: false)"
}
},
"required": ["file_path"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.insert_image",
"description": "Insert image to slide",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"image_path": {
"type": "string",
"description": "Image file path"
},
"width": {
"type": "number",
"description": "Image width in cm"
},
"height": {
"type": "number",
"description": "Image height in cm"
},
"position": {
"type": "object",
"description": "Position coordinates",
"properties": {
"x": {
"type": "number",
"description": "X position (% of slide width)"
},
"y": {
"type": "number",
"description": "Y position (% of slide height)"
}
}
}
},
"required": ["slide_index", "image_path"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.configure_display_settings",
"description": "Configure presentation display settings",
"parameters": {
"type": "object",
"properties": {
"use_presenter_view": {
"type": "boolean",
"description": "Use presenter view"
},
"primary_monitor_only": {
"type": "boolean",
"description": "Use primary monitor only"
},
"monitor_for_presentation": {
"type": "integer",
"description": "Monitor number for presentation"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_slide_number_color",
"description": "Set slide number color",
"parameters": {
"type": "object",
"properties": {
"color": {
"type": "string",
"description": "Color name or hex code"
}
},
"required": ["color"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_text_strikethrough",
"description": "Apply strikethrough formatting to text",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
},
"line_numbers": {
"type": "array",
"items": {
"type": "integer"
},
"description": "Line numbers for strikethrough (1-based)"
},
"apply": {
"type": "boolean",
"description": "Apply or remove strikethrough"
}
},
"required": ["slide_index", "box_index", "line_numbers", "apply"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.set_textbox_alignment",
"description": "Set text alignment for textbox",
"parameters": {
"type": "object",
"properties": {
"slide_index": {
"type": "integer",
"description": "Slide index (1-based)"
},
"box_index": {
"type": "integer",
"description": "Textbox index (0-based)"
},
"alignment": {
"type": "string",
"description": "Text alignment",
"enum": ["left", "center", "right", "justify"]
}
},
"required": ["slide_index", "box_index", "alignment"]
}
}
},
{
"type": "function",
"function": {
"name": "ImpressTools.export_to_image",
"description": "Export presentation or slide to image",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Image save path with filename and extension"
},
"format": {
"type": "string",
"description": "Image format",
"enum": ["png", "jpeg", "jpg", "gif", "bmp", "tiff"]
},
"slide_index": {
"type": "integer",
"description": "Specific slide index (1-based). If not provided, exports all slides"
}
},
"required": ["file_path", "format"]
}
}
}
]

View File

@@ -0,0 +1,412 @@
[
{
"type": "function",
"function": {
"name": "WriterTools.save",
"description": "Save document to current location",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.write_text",
"description": "Write text at cursor position",
"parameters": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Text to write"
},
"bold": {
"type": "boolean",
"description": "Apply bold formatting"
},
"italic": {
"type": "boolean",
"description": "Apply italic formatting"
},
"size": {
"type": "number",
"description": "Font size"
}
},
"required": ["text"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_color",
"description": "Change text color using regex pattern",
"parameters": {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "Regex pattern to match"
},
"color": {
"type": "number",
"description": "Hex color code (e.g., 0x000000)"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["pattern", "color"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.find_and_replace",
"description": "Find and replace text using regex",
"parameters": {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "Regex pattern to find"
},
"replacement": {
"type": "string",
"description": "Replacement text"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["pattern", "replacement"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_font",
"description": "Change font family",
"parameters": {
"type": "object",
"properties": {
"font_name": {
"type": "string",
"description": "Font name (e.g., 'Arial', 'Times New Roman')"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["font_name"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_line_spacing",
"description": "Set line spacing",
"parameters": {
"type": "object",
"properties": {
"spacing_value": {
"type": "number",
"description": "Spacing value (1.0=single, 2.0=double)"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["spacing_value"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.remove_highlighting",
"description": "Remove text highlighting",
"parameters": {
"type": "object",
"properties": {
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.find_highlighted_text",
"description": "Find text with specific highlight color",
"parameters": {
"type": "object",
"properties": {
"highlight_color": {
"type": "string",
"description": "Color name (e.g., 'yellow') or hex code"
}
},
"required": ["highlight_color"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.insert_formula_at_cursor",
"description": "Insert formula at cursor",
"parameters": {
"type": "object",
"properties": {
"formula": {
"type": "string",
"description": "Formula to insert"
}
},
"required": ["formula"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.insert_image_at_cursor",
"description": "Insert image at cursor",
"parameters": {
"type": "object",
"properties": {
"image_path": {
"type": "string",
"description": "Full path to image file"
},
"width": {
"type": "integer",
"description": "Display width in pixels"
},
"height": {
"type": "integer",
"description": "Display height in pixels"
}
},
"required": ["image_path"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_strikethrough",
"description": "Apply strikethrough formatting",
"parameters": {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "Regex pattern to match"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["pattern"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_font_size",
"description": "Change font size",
"parameters": {
"type": "object",
"properties": {
"font_size": {
"type": "number",
"description": "Font size in points"
},
"pattern": {
"type": "string",
"description": "Regex pattern to match"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["font_size", "pattern"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.export_to_pdf",
"description": "Export document to PDF",
"parameters": {
"type": "object",
"properties": {
"output_path": {
"type": "string",
"description": "PDF save path"
},
"output_filename": {
"type": "string",
"description": "PDF filename"
},
"include_comments": {
"type": "boolean",
"description": "Include comments in PDF"
},
"quality": {
"type": "string",
"description": "Export quality ('standard', 'high', 'print')"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_paragraph_alignment",
"description": "Set paragraph alignment",
"parameters": {
"type": "object",
"properties": {
"alignment": {
"type": "string",
"description": "Alignment type ('left', 'center', 'right', 'justify')"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["alignment"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.capitalize_words",
"description": "Capitalize first letter of each word",
"parameters": {
"type": "object",
"properties": {
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.set_default_font",
"description": "Set default font for new text",
"parameters": {
"type": "object",
"properties": {
"font_name": {
"type": "string",
"description": "Default font name"
},
"font_size": {
"type": "number",
"description": "Default font size in points"
}
},
"required": ["font_name"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.add_page_numbers",
"description": "Add page numbers",
"parameters": {
"type": "object",
"properties": {
"position": {
"type": "string",
"description": "Position ('bottom_left', 'bottom_center', 'bottom_right', 'top_left', 'top_center', 'top_right')"
},
"start_number": {
"type": "integer",
"description": "Starting page number"
},
"format": {
"type": "string",
"description": "Number format (e.g., '1', 'Page 1', '1 of N')"
}
},
"required": ["position"]
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.insert_page_break",
"description": "Insert page break",
"parameters": {
"type": "object",
"properties": {
"position": {
"type": "string",
"description": "Insert location ('at_cursor', 'end_of_document')"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "WriterTools.change_text_case",
"description": "Change text case",
"parameters": {
"type": "object",
"properties": {
"case_type": {
"type": "string",
"description": "Case type ('lowercase', 'uppercase')"
},
"pattern": {
"type": "string",
"description": "Regex pattern to match"
},
"paragraph_indices": {
"type": "array",
"description": "Target paragraph indices (0-based). Applies to all if omitted"
}
},
"required": ["case_type", "pattern"]
}
}
}
]

View File

@@ -0,0 +1,166 @@
[
{
"type": "function",
"function": {
"name": "VLCTools.get_playlist",
"description": "Get current playlist with track info",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.play",
"description": "Start playing current media",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.pause",
"description": "Pause current media",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.next",
"description": "Switch to next track",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.previous",
"description": "Switch to previous track",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.add_to_playlist",
"description": "Add media file to playlist",
"parameters": {
"type": "object",
"properties": {
"uri": {
"type": "string",
"description": "Media file URI (file:// or https://)"
}
},
"required": ["uri"]
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.get_current_time",
"description": "Get current playback position in seconds",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.get_media_duration",
"description": "Get media duration in seconds",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.toggle_fullscreen",
"description": "Toggle or set fullscreen mode",
"parameters": {
"type": "object",
"properties": {
"enable": {
"type": "boolean",
"description": "Force fullscreen on/off, omit to toggle"
}
},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.get_settings",
"description": "Get VLC settings",
"parameters": {
"type": "object",
"properties": {}
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.set_settings",
"description": "Set VLC settings",
"parameters": {
"type": "object",
"properties": {
"field": {
"type": "string",
"description": "Setting name (e.g. qt-max-volume, qt-minimal-view)"
},
"value": {
"type": "string",
"description": "Setting value (use 0/1 for booleans)"
}
},
"required": ["field", "value"]
}
}
},
{
"type": "function",
"function": {
"name": "VLCTools.get_media_files",
"description": "Get media files from path",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path"
},
"suffix": {
"type": "array",
"description": "File extensions, default: ['mp4','avi','mkv','mov','mp3','m4a','wav']"
}
},
"required": ["path"]
}
}
}
]

View File

@@ -0,0 +1,260 @@
import json
import os
import subprocess
from pathlib import Path
class CodeTools:
ret = ""
@classmethod
def print_result(cls):
"""打印执行结果"""
print(cls.ret)
@classmethod
def launch_vscode(cls, path):
"""
Launches Visual Studio Code with the specified file path or directory.
在存在的窗口中打开一个文件或目录。
Args:
path (str): 文件路径或目录。
"""
try:
subprocess.run(["code", "-r", path], check=True)
cls.ret = "Successfully launched VS Code"
except subprocess.CalledProcessError as e:
cls.ret = f"Error launching VS Code: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def env_info(cls):
cls.ret = "None"
@classmethod
def compare_files(cls, file1, file2):
"""
Compares two files in VSCode.
在VSCode中比较两个文件。
Args:
file1 (str): 第一个文件的路径。
file2 (str): 第二个文件的路径。
"""
try:
# 获取compare结果
subprocess.run(["code", "-d", file1, file2], check=True)
cls.ret = "The compared files are opened in VSCode"
except subprocess.CalledProcessError as e:
cls.ret = f"Error comparing files: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def add_folder(cls, folder):
"""
Adds a folder to the last active window in VSCode.
向VSCode的最后一个活动窗口添加文件夹。
Args:
folder (str): 文件夹路径。
"""
try:
subprocess.run(["code", "-a", folder], check=True)
cls.ret = "Successfully added folder"
except subprocess.CalledProcessError as e:
cls.ret = f"Error adding folder: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def goto_file(cls, file_path, line=1, character=1):
"""
Opens a file at a specific line and character position.
在特定行和字符的位置打开文件。
Args:
file_path (str): 文件路径。
line (int): 行号。
character (int): 字符位置。
"""
try:
command = f"{file_path}:{line}:{character}"
subprocess.run(["code", "-g", command], check=True)
cls.ret = "Successfully opened file, line: {}, character: {}".format(line, character)
except subprocess.CalledProcessError as e:
cls.ret = f"Error going to file: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def perform_merge(cls, path1, path2, base, result):
"""
Perform a three-way merge.
执行三方合并。
Args:
path1 (str): 第一版本文件路径。
path2 (str): 第二版本文件路径。
base (str): 基础版本文件路径。
result (str): 结果文件的保存路径。
"""
try:
subprocess.run(["code", "-m", path1, path2, base, result], check=True)
cls.ret = "Successfully performed merge"
except subprocess.CalledProcessError as e:
cls.ret = f"Error performing merge: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def remove_folder(cls, folder):
"""
Removes a folder from the last active window in VSCode.
在VSCode的最后一个活动窗口中移除文件夹。
Args:
folder (str): 文件夹路径。
"""
try:
subprocess.run(["code", "--remove", folder], check=True)
cls.ret = "Successfully removed folder"
except subprocess.CalledProcessError as e:
cls.ret = f"Error removing folder: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def install_extension(cls, extension_id, pre_release=False):
"""
Installs an extension or updates it in VSCode.
安装或更新VSCode中的扩展。
Args:
extension_id (str): 扩展的标识符。
pre_release (bool): 是否安装预发布版本。
"""
try:
command = ["code", "--install-extension", extension_id]
if pre_release:
command.append("--pre-release")
subprocess.run(command, check=True)
cls.ret = "Successfully installed extension"
except subprocess.CalledProcessError as e:
cls.ret = f"Error installing extension: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def uninstall_extension(cls, extension_id):
"""
Uninstalls an extension from VSCode.
从VSCode中卸载扩展。
Args:
extension_id (str): 扩展的标识符。
"""
try:
subprocess.run(["code", "--uninstall-extension", extension_id], check=True)
cls.ret = "Successfully uninstalled extension"
except subprocess.CalledProcessError as e:
cls.ret = f"Error uninstalling extension: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def list_extensions(cls, show_versions=False, category=None):
"""
Lists installed extensions in VSCode.
列出VSCode中安装的扩展。
Args:
show_versions (bool): 是否显示扩展的版本。
category (str): 按类别筛选扩展。
"""
try:
command = ["code", "--list-extensions"]
if show_versions:
command.append("--show-versions")
if category:
command.extend(["--category", category])
cls.ret = subprocess.run(command, check=True, capture_output=True, text=True).stdout
except subprocess.CalledProcessError as e:
cls.ret = f"Error listing extensions: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def update_extensions(cls):
"""
Updates all installed extensions in VSCode to the latest version.
更新VSCode中所有安装的扩展到最新版本。
"""
try:
subprocess.run(["code", "--update-extensions"], check=True)
cls.ret = "Successfully updated extensions"
except subprocess.CalledProcessError as e:
cls.ret = f"Error updating extensions: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def disable_extension(cls, extension_id):
"""
Disables a specific extension for the next instance of VSCode.
禁用在下一个VSCode窗口中的指定扩展。
Args:
extension_id (str): 扩展的标识符。
"""
try:
subprocess.run(["code", "--disable-extension", extension_id], check=True)
cls.ret = "Successfully disabled extension"
except subprocess.CalledProcessError as e:
cls.ret = f"Error disabling extension: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret
@classmethod
def toggle_sync(cls, state):
"""
Toggles synchronization on or off in VSCode.
在VSCode中开启或关闭同步。
Args:
state (str): 'on''off' 表示开启或关闭。
"""
try:
command = ["code", "--sync", state]
subprocess.run(command, check=True)
cls.ret = "Successfully toggled sync"
except subprocess.CalledProcessError as e:
cls.ret = f"Error toggling sync: {e}"
except Exception as e:
cls.ret = f"Unexpected error: {e}"
return cls.ret

View File

@@ -0,0 +1,107 @@
class BrowserTools:
ret = ""
@classmethod
def print_result(cls):
print(cls.ret)
@classmethod
def env_info(cls):
cls.ret = "None"
# @classmethod
# def show_all_tabs(cls):
# cls.ret = "Browser not found"
# for attempt in range(3):
# with sync_playwright() as p:
# try:
# browser = p.chromium.connect_over_cdp(cls.remote_debugging_url)
# if not browser:
# continue
# context = browser.contexts[0]
# # 获取所有窗口名称
# cls.ret = 'Browser Tabs: '
# for idx, page in enumerate(context.pages):
# cls.ret += f"{idx}. {page.title()} ({page.url})" + '\n'
# return cls.ret
# except TimeoutError:
# cls.ret = 'Failed to get browser tabs'
# return None
# return None
@classmethod
def open_profile_settings(cls):
"""
Open the profile settings page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://settings/people"]}}
@classmethod
def open_password_settings(cls):
"""
Open the password settings page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://settings/autofill"]}}
@classmethod
def open_privacy_settings(cls):
"""
Open the privacy settings page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://settings/privacy"]}}
@classmethod
def open_appearance_settings(cls):
"""
Open the appearance settings page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://settings/appearance"]}}
@classmethod
def open_search_engine_settings(cls):
"""
Open the search engine settings page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://settings/search"]}}
@classmethod
def bring_back_last_tab(cls):
"""
Bring back the last tab in the browser.
"""
return f"import pyautogui; pyautogui.hotkey('ctrl', 'shift', 't'); print('Brought back last tab')"
@classmethod
def print(cls):
"""
Open the print option in current page.
"""
return f"import pyautogui; pyautogui.hotkey('ctrl', 'p'); print('Opened print option')"
@classmethod
def delete_browsing_data(cls):
"""
Delete browsing data in the browser.
"""
return f"import pyautogui; pyautogui.hotkey('ctrl', 'shift', 'del'); print('Deleted browsing data')"
@classmethod
def open_extensions(cls):
"""
open the extensions page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://extensions"]}}
@classmethod
def bookmark_page(cls):
"""
Bookmark the current page in the browser.
"""
return f"import pyautogui; pyautogui.hotkey('ctrl', 'd'); print('Bookmarked page')"
@classmethod
def open_bookmarks(cls):
"""
Open the bookmarks page in the browser.
"""
return {"action_type": "OPEN_CHROME_TAB", "parameters": {"urls_to_open": ["chrome://bookmarks"]}}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,753 @@
import os
import re
import uno
from com.sun.star.awt.FontSlant import ITALIC, NONE, OBLIQUE
from com.sun.star.awt.FontWeight import BOLD, NORMAL
from com.sun.star.beans import PropertyValue
from com.sun.star.style.ParagraphAdjust import CENTER, LEFT, RIGHT
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
class WriterTools:
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
doc = desktop.getCurrentComponent()
text = doc.Text
cursor = text.createTextCursor()
ret = ""
@classmethod
def close_other_window(cls):
"""关闭除当前文档外的所有文档"""
components = cls.desktop.getComponents().createEnumeration()
current_url = cls.doc.getURL()
while components.hasMoreElements():
doc = components.nextElement()
if doc.getURL() != current_url:
doc.close(True)
@classmethod
def save(cls):
"""保存文档到当前位置"""
try:
if cls.doc.hasLocation():
cls.doc.store()
else:
raise Exception("文档没有保存位置,请使用另存为功能")
return True
except Exception as e:
return False
@classmethod
def maximize_window(cls):
"""
将窗口设置为工作区最大尺寸
使用工作区域大小(考虑任务栏等)
"""
window = cls.doc.getCurrentController().getFrame().getContainerWindow()
toolkit = window.getToolkit()
device = toolkit.createScreenCompatibleDevice(0, 0)
workarea = toolkit.getWorkArea()
window.setPosSize(workarea.X, workarea.Y, workarea.Width, workarea.Height, 15)
@classmethod
def print_result(cls):
print(cls.ret)
@classmethod
def write_text(cls, text, bold=False, italic=False, size=None):
"""写入文本"""
cls.cursor.CharWeight = 150 if bold else 100
cls.cursor.CharPosture = ITALIC if italic else NONE
if size:
cls.cursor.CharHeight = size
cls.text.insertString(cls.cursor, text, False)
cls.ret = "Success"
@classmethod
def get_paragraphs(cls, start_index=0, count=None):
"""Retrieves paragraphs from the document as a list."""
text = cls.doc.getText()
paragraphs = text.createEnumeration()
paragraph_list = []
while paragraphs.hasMoreElements():
paragraph = paragraphs.nextElement()
if paragraph.supportsService("com.sun.star.text.Paragraph"):
paragraph_list.append(paragraph.getString())
if start_index < 0:
start_index = 0
elif start_index >= len(paragraph_list):
cls.ret = []
if count is not None:
end_index = min(start_index + count, len(paragraph_list))
cls.ret = paragraph_list[start_index:end_index]
else:
cls.ret = paragraph_list[start_index:]
return cls.ret
@classmethod
def env_info(cls):
paras = cls.get_paragraphs()
para_str = ""
for i, para in enumerate(paras):
para = para[:500] + "..." if len(para) > 500 else para
para_str += "Paragraph " + str(i) + ": " + para.strip() + "\n"
cls.ret = para_str
return cls.ret
@classmethod
def set_color(cls, pattern, color, paragraph_indices=None):
"""
Changes the color of matched text in the document for specified paragraphs.
Args:
pattern (str): Regular expression pattern to match text
color (int): Hex color code (e.g., 0x000000 for black)
paragraph_indices (list, optional): List of paragraph indices to modify (0-based).
If None, applies to all paragraphs.
"""
try:
enum = cls.doc.Text.createEnumeration()
paragraphs = []
while enum.hasMoreElements():
paragraphs.append(enum.nextElement())
if not paragraph_indices:
paragraphs_to_process = range(len(paragraphs))
else:
paragraphs_to_process = paragraph_indices
regex = re.compile(pattern)
for idx in paragraphs_to_process:
if idx < 0 or idx >= len(paragraphs):
continue
paragraph = paragraphs[idx]
if not paragraph.supportsService("com.sun.star.text.Paragraph"):
continue
para_text = paragraph.getString()
matches = regex.finditer(para_text)
for match in matches:
para_cursor = cls.text.createTextCursorByRange(paragraph.getStart())
para_cursor.goRight(match.start(), False)
para_cursor.goRight(match.end() - match.start(), True)
para_cursor.CharColor = color
cls.ret = "Success"
return True
except Exception as e:
cls.ret = f"Error: {str(e)}"
return False
@classmethod
def find_and_replace(cls, pattern, replacement, paragraph_indices=None):
"""
Finds all occurrences of a specified text pattern and replaces them with another text in the document.
Args:
pattern (str): The pattern to match in the document, should be a regular expression
replacement (str): The text to replace the found text with
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing)
Returns:
str: Success message with number of replacements made
"""
try:
enum = cls.doc.Text.createEnumeration()
paragraphs = []
while enum.hasMoreElements():
paragraphs.append(enum.nextElement())
total_replacements = 0
if not paragraph_indices:
paragraphs_to_process = list(range(len(paragraphs)))
else:
paragraphs_to_process = [i for i in paragraph_indices if 0 <= i < len(paragraphs)]
regex = re.compile(pattern)
for idx in paragraphs_to_process:
if idx >= len(paragraphs):
continue
paragraph = paragraphs[idx]
if paragraph.supportsService("com.sun.star.text.Paragraph"):
text_content = paragraph.getString()
new_text, count = regex.subn(replacement, text_content)
if count > 0:
paragraph.setString(new_text)
total_replacements += count
cls.ret = f"Successfully made {total_replacements} replacements"
return cls.ret
except Exception as e:
cls.ret = f"Error during find and replace: {str(e)}"
return cls.ret
@classmethod
def set_font(cls, font_name, paragraph_indices=None):
"""
Changes the font of text in the document or specified paragraphs.
Args:
font_name (str): The name of the font to apply (e.g., 'Times New Roman', 'Arial', 'Calibri')
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
"""
try:
text = cls.doc.getText()
enum = text.createEnumeration()
paragraphs = []
while enum.hasMoreElements():
paragraphs.append(enum.nextElement())
if not paragraph_indices:
paragraph_indices = range(len(paragraphs))
for idx in paragraph_indices:
if 0 <= idx < len(paragraphs):
paragraph = paragraphs[idx]
cursor = text.createTextCursorByRange(paragraph)
cursor.CharFontName = font_name
cls.ret = "Success"
return True
except Exception as e:
cls.ret = f"Error: {str(e)}"
return False
@classmethod
def set_line_spacing(cls, spacing_value, paragraph_indices=None):
"""
Sets the line spacing for specified paragraphs in the document.
Args:
spacing_value (float): The line spacing value to apply (1.0 for single spacing, 2.0 for double spacing, etc.)
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
"""
try:
text = cls.doc.getText()
paragraph_enum = text.createEnumeration()
line_spacing_value = int(spacing_value * 100)
current_index = 0
while paragraph_enum.hasMoreElements():
paragraph = paragraph_enum.nextElement()
if not paragraph_indices or current_index in paragraph_indices:
line_spacing = uno.createUnoStruct("com.sun.star.style.LineSpacing")
line_spacing.Mode = 0
line_spacing.Height = line_spacing_value
paragraph.ParaLineSpacing = line_spacing
if paragraph.String.strip():
current_index += 1
cls.ret = "Success"
return True
except Exception as e:
cls.ret = f"Error: {str(e)}"
return False
@classmethod
def remove_highlighting(cls, paragraph_indices=None):
"""
Removes ALL highlighting from text in the document for specified paragraphs.
Args:
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
Returns:
str: Success message or error message
"""
try:
text = cls.doc.getText()
paragraphs = text.createEnumeration()
target_indices = set(paragraph_indices) if paragraph_indices else None
current_index = 0
while paragraphs.hasMoreElements():
paragraph = paragraphs.nextElement()
if target_indices is None or current_index in target_indices:
if paragraph.supportsService("com.sun.star.text.Paragraph"):
para_cursor = text.createTextCursorByRange(paragraph)
# Remove all highlighting by setting back color to -1
para_cursor.CharBackColor = -1
# Additional cleanup for individual text portions (optional)
text_portions = paragraph.createEnumeration()
while text_portions.hasMoreElements():
text_portion = text_portions.nextElement()
if hasattr(text_portion, "CharBackColor"):
portion_cursor = text.createTextCursorByRange(text_portion)
portion_cursor.CharBackColor = -1
current_index += 1
cls.ret = "Successfully removed all highlighting"
return cls.ret
except Exception as e:
cls.ret = f"Error removing highlighting: {str(e)}"
return cls.ret
@classmethod
def find_highlighted_text(cls, highlight_color):
"""
Finds all text in the document that has a specific highlight color applied to it.
Args:
highlight_color (str): The highlight color to search for. Can be a color name (e.g., 'yellow', 'green') or hex code.
Returns:
list: A list of strings containing all text segments with the specified highlight color.
"""
color_map = {
"yellow": 16776960,
"green": 65280,
"blue": 255,
"red": 16711680,
"cyan": 65535,
"magenta": 16711935,
"black": 0,
"white": 16777215,
"gray": 8421504,
"lightgray": 12632256,
}
target_color = None
if highlight_color.lower() in color_map:
target_color = color_map[highlight_color.lower()]
elif highlight_color.startswith("#") and len(highlight_color) == 7:
try:
hex_color = highlight_color[1:]
r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)
target_color = (r << 16) + (g << 8) + b
except ValueError:
cls.ret = f"Invalid hex color format: {highlight_color}"
return []
else:
cls.ret = f"Unsupported color format: {highlight_color}"
return []
highlighted_text = []
text = cls.doc.getText()
enum_paragraphs = text.createEnumeration()
while enum_paragraphs.hasMoreElements():
paragraph = enum_paragraphs.nextElement()
if paragraph.supportsService("com.sun.star.text.Paragraph"):
enum_portions = paragraph.createEnumeration()
while enum_portions.hasMoreElements():
text_portion = enum_portions.nextElement()
if hasattr(text_portion, "CharBackColor") and text_portion.CharBackColor == target_color:
if text_portion.getString().strip():
highlighted_text.append(text_portion.getString())
cls.ret = f"Found {len(highlighted_text)} text segments with highlight color {highlight_color}"
return highlighted_text
@classmethod
def insert_formula_at_cursor(cls, formula):
"""
Inserts a formula at the current cursor position in the document.
Args:
formula (str): The formula to insert at the current cursor position.
Returns:
bool: True if successful, False otherwise
"""
try:
embedded_obj = cls.doc.createInstance("com.sun.star.text.TextEmbeddedObject")
embedded_obj.setPropertyValue("CLSID", "078B7ABA-54FC-457F-8551-6147e776a997")
embedded_obj.setPropertyValue("AnchorType", AS_CHARACTER)
cls.text.insertTextContent(cls.cursor, embedded_obj, False)
math_obj = embedded_obj.getEmbeddedObject()
math_obj.Formula = formula
cls.ret = "Formula inserted successfully"
return True
except Exception as e:
cls.ret = f"Error inserting formula: {str(e)}"
return False
@classmethod
def insert_image_at_cursor(cls, image_path, width=None, height=None):
"""
Inserts an image at the current cursor position in the document.
Args:
image_path (str): Full path to the image file to insert
width (int, optional): Width to display the image in pixels
height (int, optional): Height to display the image in pixels
Returns:
str: Success message or error message
"""
try:
if image_path.startswith("~"):
image_path = os.path.expanduser(image_path)
if not os.path.exists(image_path):
cls.ret = f"Error: Image file not found at {image_path}"
return cls.ret
image_path = os.path.abspath(image_path)
if os.name == "nt":
file_url = "file:///" + image_path.replace("\\", "/")
else:
file_url = "file://" + image_path
graphic = cls.doc.createInstance("com.sun.star.text.GraphicObject")
graphic.GraphicURL = file_url
graphic.AnchorType = AS_CHARACTER
if width is not None:
graphic.Width = width * 100
if height is not None:
graphic.Height = height * 100
cls.text.insertTextContent(cls.cursor, graphic, False)
cls.ret = "Success: Image inserted"
return cls.ret
except Exception as e:
cls.ret = f"Error: {str(e)}"
return cls.ret
@classmethod
def set_strikethrough(cls, pattern, paragraph_indices=None):
"""
Sets the strikethrough formatting for text matching the specified pattern in the document.
Args:
pattern (str): The regular expression pattern to match in the document
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
Returns:
str: Success message or error information
"""
try:
paragraphs = cls.doc.getText().createEnumeration()
para_index = 0
found_matches = 0
while paragraphs.hasMoreElements():
paragraph = paragraphs.nextElement()
if paragraph.supportsService("com.sun.star.text.Paragraph"):
if paragraph_indices and para_index not in paragraph_indices:
para_index += 1
continue
para_text = paragraph.getString()
matches = list(re.finditer(pattern, para_text))
for match in matches:
text_range = paragraph.getStart()
cursor = cls.doc.getText().createTextCursorByRange(text_range)
cursor.goRight(match.start(), False)
cursor.goRight(match.end() - match.start(), True)
cursor.CharStrikeout = 1
found_matches += 1
para_index += 1
cls.ret = f"Successfully applied strikethrough to {found_matches} matches of pattern: {pattern}"
return cls.ret
except Exception as e:
cls.ret = f"Error applying strikethrough: {str(e)}"
return cls.ret
@classmethod
def set_font_size(cls, font_size, pattern, paragraph_indices=None):
"""
Changes the font size of specified text in the document.
Args:
font_size (float): The font size to apply (in points).
pattern (str): The pattern to match in the document, should be a regular expression.
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
Returns:
str: Result message indicating success or failure.
"""
try:
regex = re.compile(pattern)
paragraphs = cls.doc.getText().createEnumeration()
current_index = 0
while paragraphs.hasMoreElements():
paragraph = paragraphs.nextElement()
if paragraph_indices and current_index not in paragraph_indices:
current_index += 1
continue
if paragraph.supportsService("com.sun.star.text.Paragraph"):
para_cursor = cls.text.createTextCursorByRange(paragraph)
para_text = paragraph.getString()
matches = list(regex.finditer(para_text))
for match in reversed(matches):
start_pos = match.start()
end_pos = match.end()
para_cursor.gotoStart(False)
para_cursor.goRight(start_pos, False)
para_cursor.goRight(end_pos - start_pos, True)
para_cursor.CharHeight = font_size
current_index += 1
cls.ret = f"Successfully changed font size to {font_size} for text matching '{pattern}'"
return cls.ret
except Exception as e:
cls.ret = f"Error changing font size: {str(e)}"
return cls.ret
@classmethod
def export_to_pdf(cls, output_path=None, output_filename=None, include_comments=False, quality="standard"):
"""
Exports the current document to PDF format.
Args:
output_path (str, optional): The full path where the PDF should be saved.
If not provided, uses the same location as the original document.
output_filename (str, optional): The filename to use for the PDF.
If not provided, uses the original document's filename with .pdf extension.
include_comments (bool, optional): Whether to include comments in the exported PDF.
Defaults to False.
quality (str, optional): The quality of the PDF export ('standard', 'high', 'print').
Defaults to 'standard'.
Returns:
str: Path to the exported PDF file or error message
"""
try:
doc_url = cls.doc.getURL()
if not doc_url and not output_path:
return "Error: Document has not been saved and no output path provided"
if doc_url:
doc_path = uno.fileUrlToSystemPath(os.path.dirname(doc_url))
doc_filename = os.path.basename(doc_url)
doc_name = os.path.splitext(doc_filename)[0]
else:
doc_path = ""
doc_name = "export"
final_path = output_path if output_path else doc_path
final_filename = output_filename if output_filename else f"{doc_name}.pdf"
if not final_filename.lower().endswith(".pdf"):
final_filename += ".pdf"
full_output_path = os.path.join(final_path, final_filename)
output_url = uno.systemPathToFileUrl(full_output_path)
export_props = []
if quality == "high":
export_props.append(PropertyValue(Name="SelectPdfVersion", Value=1))
elif quality == "print":
export_props.append(PropertyValue(Name="SelectPdfVersion", Value=2))
else:
export_props.append(PropertyValue(Name="SelectPdfVersion", Value=0))
export_props.append(PropertyValue(Name="ExportNotes", Value=include_comments))
export_props.extend(
[
PropertyValue(Name="FilterName", Value="writer_pdf_Export"),
PropertyValue(Name="Overwrite", Value=True),
]
)
cls.doc.storeToURL(output_url, tuple(export_props))
cls.ret = f"PDF exported to: {full_output_path}"
return full_output_path
except Exception as e:
cls.ret = f"Error exporting to PDF: {str(e)}"
return cls.ret
@classmethod
def set_paragraph_alignment(cls, alignment, paragraph_indices=None):
"""
Sets the text alignment for specified paragraphs in the document.
Args:
alignment (str): The alignment to apply ('left', 'center', 'right', 'justify').
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
Returns:
str: Success message or error message
"""
try:
alignment_map = {"left": LEFT, "center": CENTER, "right": RIGHT, "justify": 3}
if alignment.lower() not in alignment_map:
cls.ret = f"Error: Invalid alignment '{alignment}'. Use 'left', 'center', 'right', or 'justify'."
return cls.ret
alignment_value = alignment_map[alignment.lower()]
text = cls.doc.getText()
paragraph_enum = text.createEnumeration()
paragraphs = []
while paragraph_enum.hasMoreElements():
paragraph = paragraph_enum.nextElement()
if paragraph.supportsService("com.sun.star.text.Paragraph"):
paragraphs.append(paragraph)
if paragraph_indices:
valid_indices = [i for i in paragraph_indices if 0 <= i < len(paragraphs)]
if len(valid_indices) != len(paragraph_indices):
cls.ret = f"Warning: Some paragraph indices were out of range (0-{len(paragraphs) - 1})"
for idx in valid_indices:
paragraphs[idx].ParaAdjust = alignment_value
else:
for paragraph in paragraphs:
paragraph.ParaAdjust = alignment_value
cls.ret = f"Successfully applied '{alignment}' alignment to paragraphs"
return cls.ret
except Exception as e:
cls.ret = f"Error setting paragraph alignment: {str(e)}"
return cls.ret
@classmethod
def capitalize_words(cls, paragraph_indices=None):
"""
Capitalizes the first letter of each word for specified paragraphs in the document.
Args:
paragraph_indices (list, optional): Indices of paragraphs to modify (0-based indexing).
If not provided, applies to all paragraphs.
Returns:
str: Success message or error message
"""
try:
text = cls.doc.getText()
enum = text.createEnumeration()
paragraphs = []
while enum.hasMoreElements():
paragraph = enum.nextElement()
if paragraph.supportsService("com.sun.star.text.Paragraph"):
paragraphs.append(paragraph)
if not paragraph_indices:
target_paragraphs = list(range(len(paragraphs)))
else:
target_paragraphs = paragraph_indices
valid_indices = [idx for idx in target_paragraphs if 0 <= idx < len(paragraphs)]
for idx in valid_indices:
paragraph = paragraphs[idx]
text_content = paragraph.getString()
if not text_content.strip():
continue
capitalized_text = " ".join(word.capitalize() if word else "" for word in text_content.split(" "))
para_cursor = text.createTextCursorByRange(paragraph.getStart())
para_cursor.gotoRange(paragraph.getEnd(), True)
para_cursor.setString(capitalized_text)
cls.ret = f"Successfully capitalized words in {len(valid_indices)} paragraphs"
return cls.ret
except Exception as e:
cls.ret = f"Error capitalizing words: {str(e)}"
return cls.ret
@classmethod
def set_default_font(cls, font_name, font_size=None):
"""
Sets the default font for new text in the document without changing existing text.
Args:
font_name (str): The name of the font to set as default (e.g., 'Times New Roman', 'Arial', 'Calibri')
font_size (float, optional): The default font size in points.
Returns:
str: Success message or error message
"""
try:
style_families = cls.doc.getStyleFamilies()
paragraph_styles = style_families.getByName("ParagraphStyles")
default_style_names = ["Default", "Standard", "Normal"]
standard_style = None
for style_name in default_style_names:
if paragraph_styles.hasByName(style_name):
standard_style = paragraph_styles.getByName(style_name)
break
if standard_style is None:
style_names = paragraph_styles.getElementNames()
if style_names:
standard_style = paragraph_styles.getByName(style_names[0])
else:
raise Exception("Could not find default paragraph style")
standard_style.setPropertyValue("CharFontName", font_name)
standard_style.setPropertyValue("CharFontNameAsian", font_name)
standard_style.setPropertyValue("CharFontNameComplex", font_name)
if font_size is not None:
standard_style.setPropertyValue("CharHeight", float(font_size))
standard_style.setPropertyValue("CharHeightAsian", float(font_size))
standard_style.setPropertyValue("CharHeightComplex", float(font_size))
cls.cursor.setPropertyValue("CharFontName", font_name)
cls.cursor.setPropertyValue("CharFontNameAsian", font_name)
cls.cursor.setPropertyValue("CharFontNameComplex", font_name)
if font_size is not None:
cls.cursor.setPropertyValue("CharHeight", float(font_size))
cls.cursor.setPropertyValue("CharHeightAsian", float(font_size))
cls.cursor.setPropertyValue("CharHeightComplex", float(font_size))
cls.ret = f"Default font set to '{font_name}'" + (f" with size {font_size}pt" if font_size else "")
return cls.ret
except Exception as e:
cls.ret = f"Error setting default font: {str(e)}"
return cls.ret
@classmethod
def add_page_numbers(cls, position, start_number=1, format=None):
"""
Adds page numbers to the document at the specified position.
Args:
position (str): Position of the page numbers ('bottom_left', 'bottom_center', 'bottom_right',
'top_left', 'top_center', 'top_right')
start_number (int, optional): The starting page number. Defaults to 1.
format (str, optional): Format of the page numbers (e.g., '1', 'Page 1', '1 of N').
Defaults to simple number format.
Returns:
str: Success message or error message
"""
try:
page_styles = cls.doc.StyleFamilies.getByName("PageStyles")
default_style = page_styles.getByName("Standard")
try:
default_style.setPropertyValue("PageNumberOffset", start_number)
except:
pass
if position.startswith("top"):
default_style.HeaderIsOn = True
target = default_style.HeaderText
else:
default_style.FooterIsOn = True
target = default_style.FooterText
cursor = target.createTextCursor()
cursor.gotoStart(False)
cursor.gotoEnd(True)
cursor.setString("")
cursor.gotoStart(False)
if position.endswith("_left"):
cursor.ParaAdjust = LEFT
elif position.endswith("_center"):
cursor.ParaAdjust = CENTER
elif position.endswith("_right"):
cursor.ParaAdjust = RIGHT
if not format or format == "1":
page_number = cls.doc.createInstance("com.sun.star.text.TextField.PageNumber")
page_number.NumberingType = 4
target.insertTextContent(cursor, page_number, False)
elif format == "Page 1" or "Page" in format and "of" not in format:
target.insertString(cursor, "Page ", False)
page_number = cls.doc.createInstance("com.sun.star.text.TextField.PageNumber")
page_number.NumberingType = 4
target.insertTextContent(cursor, page_number, False)
elif format == "1 of N" or format == "Page {page} of {total}" or "of" in format:
if "Page" in format:
target.insertString(cursor, "Page ", False)
page_number = cls.doc.createInstance("com.sun.star.text.TextField.PageNumber")
page_number.NumberingType = 4
target.insertTextContent(cursor, page_number, False)
target.insertString(cursor, " of ", False)
page_count = cls.doc.createInstance("com.sun.star.text.TextField.PageCount")
page_count.NumberingType = 4
target.insertTextContent(cursor, page_count, False)
else:
page_number = cls.doc.createInstance("com.sun.star.text.TextField.PageNumber")
page_number.NumberingType = 4
target.insertTextContent(cursor, page_number, False)
cls.ret = "Successfully added page numbers"
return cls.ret
except Exception as e:
cls.ret = f"Error adding page numbers: {str(e)}"
return cls.ret
@classmethod
def insert_page_break(cls, position="at_cursor"):
"""
Inserts a page break at the specified position.
Args:
position (str): Where to insert the page break: 'at_cursor' for current cursor position,
'end_of_document' for end of document. Defaults to 'at_cursor'.
"""
try:
if position == "end_of_document":
cls.cursor.gotoEnd(False)
cls.text.insertControlCharacter(cls.cursor, PARAGRAPH_BREAK, False)
cls.cursor.gotoStartOfParagraph(True)
cls.cursor.BreakType = uno.Enum("com.sun.star.style.BreakType", "PAGE_BEFORE")
cls.ret = "Page break inserted successfully"
return True
except Exception as e:
cls.ret = f"Error inserting page break: {str(e)}"
return False

View File

@@ -0,0 +1,233 @@
import json
import os
import re
import xml.etree.ElementTree as ET
from pathlib import Path
from urllib.parse import quote
import requests
from requests.auth import HTTPBasicAuth
class VLCTools:
host = "localhost"
port = 8080
base_url = f"http://{host}:{port}/requests"
password = "password"
auth = HTTPBasicAuth("", password)
ret = ""
@classmethod
def print_result(cls):
print(cls.ret)
@classmethod
def _make_request(cls, endpoint, params=None):
url = f"{cls.base_url}/{endpoint}"
try:
response = requests.get(url, params=params, auth=cls.auth)
response.raise_for_status()
return response
except requests.exceptions.RequestException as e:
return None
@classmethod
def _get_status(cls):
response = cls._make_request("status.xml")
if response:
return ET.fromstring(response.content)
return None
@classmethod
def env_info(cls):
cls.ret = "None"
@classmethod
def get_playlist(cls):
response = cls._make_request("playlist.xml")
if response:
info = ET.fromstring(response.content)
playlist_node = info.find('.//node[@name="Playlist"]')
if playlist_node is not None:
playlist_items = []
for leaf in playlist_node.findall("leaf"):
item = {"name": leaf.get("name"), "uri": leaf.get("uri"), "duration": leaf.get("duration") + "s"}
playlist_items.append(item)
cls.ret = f"Playlist: {playlist_items}"
return cls.ret
cls.ret = "Error getting playlist"
return None
@classmethod
def play(cls):
response = cls._make_request("status.xml", {"command": "pl_play"})
if response:
cls.ret = "Start playing the media"
return cls.ret
cls.ret = "Error playing the media"
return None
@classmethod
def pause(cls):
response = cls._make_request("status.xml", {"command": "pl_pause"})
if response:
cls.ret = "Pause the media"
return cls.ret
cls.ret = "Error pausing the media"
return None
@classmethod
def next(cls):
response = cls._make_request("status.xml", {"command": "pl_next"})
if response:
cls.ret = "Switch to next media"
return cls.ret
cls.ret = "Error switching to next media"
return None
@classmethod
def previous(cls):
response = cls._make_request("status.xml", {"command": "pl_previous"})
if response:
cls.ret = "Switch to previous media"
return cls.ret
cls.ret = "Error switching to previous media"
return None
@classmethod
def add_to_playlist(cls, uri):
if uri.startswith("http"):
encoded_uri = uri
else:
encoded_uri = "file://" + quote(uri.replace("file://", ""))
response = cls._make_request("status.xml", {"command": "in_play", "input": encoded_uri})
if response:
cls.ret = f"Add {uri} to playlist"
return cls.ret
cls.ret = f"Error adding {uri} to playlist"
return None
@classmethod
def get_current_time(cls):
status = cls._get_status()
if status is not None:
time = status.find("time")
cls.ret = int(time.text) if time is not None else None
return cls.ret
return None
@classmethod
def get_media_duration(cls):
status = cls._get_status()
if status is not None:
length = status.find("length")
if length is not None:
cls.ret = f"Media duration: {length.text} seconds"
return cls.ret
cls.ret = "Error getting media duration"
return None
@classmethod
def get_settings(cls):
settings = {}
with open(Path.home() / ".config/vlc/vlcrc", "r") as f:
for line in f:
if line:
try:
key, value = line.split("=")
if key.strip().startswith("#"):
continue
settings[key.strip()] = value.strip()
except:
continue
cls.ret = json.dumps(settings, indent=4, ensure_ascii=False)
return cls.ret
@classmethod
def set_settings(cls, field, value):
with open(Path.home() / ".config/vlc/vlcrc", "r") as rf:
settings = rf.read()
# 正则表达式匹配settings中的field项并替换
pattern = re.compile(r"#? *" + re.escape(field) + r"=.*")
# 判断是否存在field项
if pattern.search(settings):
settings = pattern.sub(f"{field}={value}", settings)
else:
settings += f"{field}={value}\n"
with open(Path.home() / ".config/vlc/vlcrc", "w") as wf:
wf.write(settings)
cls.ret = f"Set {field} to {value}"
return cls.ret
@classmethod
def toggle_fullscreen(cls, enable=None):
"""
Toggle fullscreen mode or set it explicitly based on the enable parameter.
Args:
enable (bool, optional): If provided, explicitly set fullscreen mode (True for fullscreen, False for windowed)
Returns:
str: Success or error message
"""
if enable is not None:
command = "fullscreen" if enable else "fullscreen off"
else:
command = "fullscreen"
response = cls._make_request("status.xml", {"command": command})
if response:
action = "enabled" if enable is True else "disabled" if enable is False else "toggled"
cls.ret = f"Fullscreen mode {action}"
return cls.ret
cls.ret = "Error changing fullscreen mode"
return None
@classmethod
def get_media_files(cls, path, suffix=None):
"""
Gets the media files for the specified path.
Args:
path (str): The path to the media files
suffix (List[str], optional): The suffix of the media files.
Defaults to ['mp4', 'avi', 'mkv', 'mov', 'mp3', 'm4a', 'wav']
"""
# Set default suffix if not provided
if suffix is None:
suffix = ["mp4", "avi", "mkv", "mov", "mp3", "m4a", "wav"]
# Validate path
if not path:
cls.ret = "Path cannot be empty"
return None
if not os.path.exists(path):
cls.ret = f"Path not found: {path}"
return None
# Initialize result list
media_files = []
# Convert suffix list to lowercase for case-insensitive comparison
suffix = [s.lower() for s in suffix]
# Walk through directory
try:
for root, _, files in os.walk(path):
for file in files:
# Check if file extension matches any of the specified suffixes
if any(file.lower().endswith(f".{s}") for s in suffix):
# Add full path of the file to results
full_path = os.path.join(root, file)
media_files.append(full_path)
except Exception as e:
cls.ret = f"Error while scanning directory: {str(e)}"
return None
cls.ret = media_files
return cls.ret