The ClipboardMonitor system monitors the user's clipboard and processes copied text, ensuring that the document formatting is preserved. It uses LibreOffice via the UNO API to manipulate .docx
files, converting HTML to DOCX and copying the formatted content to the clipboard.
Expected system flow:
Clipboard Monitoring:
- Clears the clipboard to avoid duplication.
- Automatically detects when new text is copied.
HTML Processing:
- Saves the copied text as an
.html
file. - Formats the HTML to ensure compatibility.
- Saves the copied text as an
Conversion to DOCX:
- Uses LibreOffice to convert HTML to DOCX.
Opening and Copying in LibreOffice:
- Connects to LibreOffice in headless mode.
- Opens the
.docx
document. - Copies the formatted content to the clipboard.
Pasting the Formatted Text:
- The user can paste the text into any other program while maintaining formatting.
However, I am facing an issue that I cannot resolve. When trying to open the .docx
document using the LibreOffice UNO API, I receive the following error:
❌ Error copying from LibreOffice: expected str, bytes or os.PathLike object, not pyuno
This Python script reproduces the issue by:
- Starting LibreOffice in headless mode.
- Opening a .docx file.
- Trying to copy its content.
import os
import time
import pyperclip
from pathlib import Path
# Configura as variáveis de ambiente do UNO para que ele seja encontrado
os.environ["URE_BOOTSTRAP"] = "vnd.sun.star.pathname:C:/Program Files/LibreOffice/program/fundamental.ini"
os.environ["UNO_PATH"] = "C:/Program Files/LibreOffice/program"
os.environ["PATH"] += os.pathsep + os.environ["UNO_PATH"]
sys.path.append(os.environ["UNO_PATH"])
from ooodev.loader.lo import Lo
from ooodev.write import Write
# Path to the DOCX file
DOCX_PATH = "C:\\caminho_arquivo_exemplo\\output.docx"
def copy_from_libreoffice(docx_file):
"""Open LibreOffice and copy formatted text."""
try:
print("
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744730513a4590439.html
评论列表(0条)