feat: change find position in table api
This commit is contained in:
parent
b835ed6cb3
commit
88db5957d2
129
BookmarkOP.py
129
BookmarkOP.py
@ -1,5 +1,17 @@
|
|||||||
import json
|
import json
|
||||||
|
import uno
|
||||||
|
import base64
|
||||||
|
import io
|
||||||
|
from com.sun.star.text import XTextContent
|
||||||
|
from com.sun.star.text import XTextRange
|
||||||
|
from com.sun.star.text import XTextDocument
|
||||||
|
from com.sun.star.text import XTextCursor
|
||||||
|
from com.sun.star.text import XText
|
||||||
|
from com.sun.star.text import XBookmarksSupplier
|
||||||
|
from com.sun.star.text import XTextGraphicObjectsSupplier
|
||||||
|
from com.sun.star.graphic import XGraphicProvider
|
||||||
|
from com.sun.star.graphic import XGraphic
|
||||||
|
from com.sun.star.beans import PropertyValue
|
||||||
|
|
||||||
def Replace(bookmark_name_value_map):
|
def Replace(bookmark_name_value_map):
|
||||||
"""
|
"""
|
||||||
@ -520,8 +532,10 @@ def ReplaceTextAndInsertTableRow(json_str):
|
|||||||
|
|
||||||
def ReplaceTextAndInsertTableRowWithContentControl(json_str):
|
def ReplaceTextAndInsertTableRowWithContentControl(json_str):
|
||||||
data = json.loads(json_str)
|
data = json.loads(json_str)
|
||||||
# BatchInsertRowWithContentControl(data["table"])
|
SetDocumentToFormMode()
|
||||||
ReplaceBookmarksWithControls(data["text"])
|
Replace(data["text"])
|
||||||
|
BatchInsertRowWithContentControl(data["table"])
|
||||||
|
ReplaceBookmarksWithControls({})
|
||||||
|
|
||||||
|
|
||||||
def returnWithJSON(data):
|
def returnWithJSON(data):
|
||||||
@ -545,6 +559,30 @@ def SaveDocument():
|
|||||||
print("err:", e)
|
print("err:", e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def SetDocumentToFormMode():
|
||||||
|
"""
|
||||||
|
将文档设置为限制编辑模式,启用强制保护,仅允许填写窗体
|
||||||
|
"""
|
||||||
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
|
|
||||||
|
# 获取文档的控制器
|
||||||
|
controller = doc.getCurrentController()
|
||||||
|
|
||||||
|
# 获取文档的保护设置
|
||||||
|
protectable = doc.Protectable
|
||||||
|
|
||||||
|
# 启用强制保护
|
||||||
|
if not protectable.isProtected():
|
||||||
|
protectable.protect("") # 空字符串表示无密码保护
|
||||||
|
|
||||||
|
# 设置编辑限制为仅允许填写窗体
|
||||||
|
text_doc = doc.TextDocument
|
||||||
|
text_doc.setPropertyValue("EnableFormEdit", True)
|
||||||
|
text_doc.setPropertyValue("EnableFormFieldsOnly", True)
|
||||||
|
|
||||||
|
# 刷新视图
|
||||||
|
controller.refresh()
|
||||||
|
|
||||||
|
|
||||||
def ReplaceBookmarksWithControls(bookmark_name_value_map={}):
|
def ReplaceBookmarksWithControls(bookmark_name_value_map={}):
|
||||||
# 获取文档对象
|
# 获取文档对象
|
||||||
@ -656,6 +694,89 @@ def NextEditableZone(current_index):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"err: {str(e)}") from e
|
raise RuntimeError(f"err: {str(e)}") from e
|
||||||
|
|
||||||
|
def ReplaceBookmarkWithImage(data_array):
|
||||||
|
""" 替换书签为图片 """
|
||||||
|
# 获取当前文档
|
||||||
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
|
|
||||||
|
# 获取书签供应商
|
||||||
|
bookmarks = doc.getBookmarks()
|
||||||
|
|
||||||
|
# 获取服务管理器
|
||||||
|
smgr = XSCRIPTCONTEXT.getComponentContext().getServiceManager()
|
||||||
|
|
||||||
|
# 创建图形提供者服务
|
||||||
|
graphic_provider = smgr.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", XSCRIPTCONTEXT.getComponentContext())
|
||||||
|
|
||||||
|
for data in data_array:
|
||||||
|
bookmark_name = data["bookmarkName"]
|
||||||
|
image_data = data["imageData"]
|
||||||
|
width = data["width"]
|
||||||
|
height = data["height"]
|
||||||
|
file_type = data["fileType"]
|
||||||
|
|
||||||
|
if bookmark_name in bookmarks:
|
||||||
|
# 获取书签的锚点
|
||||||
|
bookmark = bookmarks[bookmark_name]
|
||||||
|
text_range = bookmark.getAnchor()
|
||||||
|
text_cursor = text_range.getText().createTextCursorByRange(text_range)
|
||||||
|
|
||||||
|
# 解码Base64图片数据
|
||||||
|
image_bytes = base64.b64decode(image_data)
|
||||||
|
image_stream = io.BytesIO(image_bytes)
|
||||||
|
|
||||||
|
# 创建PropertyValue对象
|
||||||
|
url_prop = PropertyValue()
|
||||||
|
url_prop.Name = "URL"
|
||||||
|
url_prop.Value = "private:stream"
|
||||||
|
|
||||||
|
input_stream_prop = PropertyValue()
|
||||||
|
input_stream_prop.Name = "InputStream"
|
||||||
|
input_stream_prop.Value = image_stream
|
||||||
|
|
||||||
|
mime_type_prop = PropertyValue()
|
||||||
|
mime_type_prop.Name = "MimeType"
|
||||||
|
mime_type_prop.Value = f"image/{file_type}"
|
||||||
|
|
||||||
|
# 将PropertyValue对象放入序列中
|
||||||
|
graphic_properties = (url_prop, input_stream_prop, mime_type_prop)
|
||||||
|
|
||||||
|
# 查询图形
|
||||||
|
graphic = graphic_provider.queryGraphic(graphic_properties)
|
||||||
|
|
||||||
|
# 创建图形对象
|
||||||
|
graphic_object = doc.createInstance("com.sun.star.text.TextGraphicObject")
|
||||||
|
graphic_object.Graphic = graphic # 设置图形属性
|
||||||
|
graphic_object.Width = width # 设置宽度
|
||||||
|
graphic_object.Height = height # 设置高度
|
||||||
|
|
||||||
|
# 替换书签为图片
|
||||||
|
text_cursor.getText().insertTextContent(text_cursor, graphic_object, False)
|
||||||
|
|
||||||
|
def GetBookmarksInLists():
|
||||||
|
"""
|
||||||
|
返回文档中所有位于列表中的书签
|
||||||
|
Returns:
|
||||||
|
list: 位于列表中的书签名称列表
|
||||||
|
"""
|
||||||
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
|
bookmarks = doc.getBookmarks()
|
||||||
|
bookmarks_in_lists = []
|
||||||
|
|
||||||
|
# 遍历所有书签
|
||||||
|
for bookmark in bookmarks:
|
||||||
|
bookmark_name = bookmark.getName()
|
||||||
|
anchor = bookmark.getAnchor()
|
||||||
|
|
||||||
|
# 判断书签的锚点是否在列表中
|
||||||
|
if anchor.supportsService("com.sun.star.text.Paragraph"):
|
||||||
|
paragraph = anchor
|
||||||
|
if paragraph.getPropertySetInfo().hasPropertyByName("NumberingStyleName"):
|
||||||
|
numbering_style = paragraph.getPropertyValue("NumberingStyleName")
|
||||||
|
if numbering_style:
|
||||||
|
bookmarks_in_lists.append(bookmark_name)
|
||||||
|
|
||||||
|
return bookmarks_in_lists
|
||||||
|
|
||||||
g_exportedScripts = (
|
g_exportedScripts = (
|
||||||
Replace,
|
Replace,
|
||||||
@ -671,4 +792,6 @@ g_exportedScripts = (
|
|||||||
ReplaceTextAndInsertTableRowWithContentControl,
|
ReplaceTextAndInsertTableRowWithContentControl,
|
||||||
SaveDocument,
|
SaveDocument,
|
||||||
NextEditableZone,
|
NextEditableZone,
|
||||||
|
GetBookmarksInLists,
|
||||||
|
SetDocumentToFormMode
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user