I have 3 .odt files with just one page and content filling this entire page. I need to merge these files into a single three-page file.
I haven’t found any native solution in python in libre office. If it did, it would be better.
The solution I’ve found so far was to create a blank main.odt file with the macro below. The bad thing about using ‘.uno:InsertDoc’ is that it inserts the text on the next line not the current line. So there is always a blank line before my text content.
This way the file has 6 pages 3 of content and 3 blank between the content pages.
Would it be possible to make ‘.uno:InsertDoc’ insert the text content without leaving that line blank?
Or is there a better solution?
my Macro:
# -*- coding: UTF-8 -*-
from __future__ import absolute_import, unicode_literals
import uno
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
from pathlib import Path
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
ODOC = XSCRIPTCONTEXT.getDocument()
def create_instance(name, with_context=False):
if with_context:
instance = SM.createInstanceWithContext(name, CTX)
else:
instance = SM.createInstance(name)
return instance
def call_dispatch(doc, url, args=()):
frame = doc.getCurrentController().getFrame()
dispatch = create_instance('com.sun.star.frame.DispatchHelper')
dispatch.executeDispatch(frame, url, '', 0, args)
return
def set_page_style(lm=500, rm=500, tm=500, bm=500, landscape=False, pw=21000, ph=29700):
oViewCursor = ODOC.CurrentController.getViewCursor()
pageStyle = oViewCursor.PageStyleName
oStyle = ODOC.StyleFamilies.getByName("PageStyles").getByName(pageStyle)
oStyle.LeftMargin = lm
oStyle.RightMargin = rm
oStyle.TopMargin = tm
oStyle.BottomMargin = bm
oStyle.IsLandscape = landscape
oStyle.Width = pw
oStyle.Height = ph
def get_odt_files(myfilter='**/*'):
url = ODOC.URL
systempath = uno.fileUrlToSystemPath(url)
absolute_path = Path(systempath).parent
files_odt = list(filter(Path.is_file, absolute_path.glob(myfilter)))
lst_file_str = []
for file_path in files_odt:
if file_path != Path(systempath):
lst_file_str.append('file:///' + str(file_path))
return lst_file_str
def merge_odt(*args):
set_page_style(lm=500, rm=0, tm=1300, bm=0)
lst_files = get_odt_files('*.odt')
struct = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
total = len(lst_files)
for i in range(total):
struct.Name = 'Name'
struct.Value = lst_files[i]
call_dispatch(ODOC, '.uno:InsertDoc', tuple([struct]))
if i != total-1:
call_dispatch(ODOC, '.uno:InsertPagebreak')
#call_dispatch(ODOC, '.uno:Print')
Complement
1 page file to be merged:
text1.odt