Opening new sheet from file w/ python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
paradoxlost
Posts: 3
Joined: Tue Apr 14, 2020 5:22 am

Opening new sheet from file w/ python

Post by paradoxlost »

i need to know the command to open a new sheet from a file in open office using python .

can anyone help me out
OpenOffice 4.1.7 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Opening new sheet from file w/ python

Post by Zizi64 »

Do you want add a new empty Worksheet in the actual file, or you want import some data from an another file into a sheet of the actual file, or do you want create a new Spreadsheet file based on the default (or other) template?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Opening new sheet from file w/ python

Post by Villeroy »

LibreOffice provides a simple method importSheet(oDoc, sName, nPosition).

With OpenOffice things are more complicated. You create a new sheet and modify its properties LinkMode, LinkURL and LinkSheetName

Code: Select all

Function getImportedSheet(oDoc, sURL$, sSheetName$, sNewSheet$, nPos%, nMode%, bUnlink AS Boolean)
   oSheets = oDoc.getSheets()
REM any existing sheet link will be refreshed
   if not oSheets.hasByName(sNewSheet) then
      oSheets.insertNewByName(sNewSheet, nPos)
   endif
   oSheet = oSheets.getByName(sNewSheet)
   oSheet.setLinkURL(sURL)
   oSheet.setLinkSheetName(sSheetName)
   oSheet.setLinkMode(nMode)
   if bUnlink then oSheet.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
getImportedSheet = oSheet
End Function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
paradoxlost
Posts: 3
Joined: Tue Apr 14, 2020 5:22 am

Re: Opening new sheet from file w/ python

Post by paradoxlost »

Zizi64 wrote:Do you want add a new empty Worksheet in the actual file, or you want import some data from an another file into a sheet of the actual file, or do you want create a new Spreadsheet file based on the default (or other) template?
I want to create a new sheet in an existing file that contains an already existing file. Basically I need to conde see down a bunch of directories into files.
Villeroy wrote:LibreOffice provides a simple method importSheet(oDoc, sName, nPosition).

With OpenOffice things are more complicated. You create a new sheet and modify its properties LinkMode, LinkURL and LinkSheetName

Code: Select all

Function getImportedSheet(oDoc, sURL$, sSheetName$, sNewSheet$, nPos%, nMode%, bUnlink AS Boolean)
   oSheets = oDoc.getSheets()
REM any existing sheet link will be refreshed
   if not oSheets.hasByName(sNewSheet) then
      oSheets.insertNewByName(sNewSheet, nPos)
   endif
   oSheet = oSheets.getByName(sNewSheet)
   oSheet.setLinkURL(sURL)
   oSheet.setLinkSheetName(sSheetName)
   oSheet.setLinkMode(nMode)
   if bUnlink then oSheet.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
getImportedSheet = oSheet
End Function
What about for libre office? Is there any more in depth documentation.
OpenOffice 4.1.7 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Opening new sheet from file w/ python

Post by Zizi64 »

What about for libre office?
The LibreOffice is a fork of the OpenOffice, with a more active developer team. The LO has more features and higher compatibility with the foreign file types.
https://www.libreoffice.org/

Both of these office suites are free and open source.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
MrProgrammer
Moderator
Posts: 4894
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Opening new sheet from file w/ python

Post by MrProgrammer »

Hi, and welcome to the forum.
paradoxlost wrote:Basically I need to conde see down a bunch of directories into files.
Why didn't you say so in the first place? XY Problem

Merge sheets of spreadsheet documents

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
paradoxlost
Posts: 3
Joined: Tue Apr 14, 2020 5:22 am

Re: Opening new sheet from file w/ python

Post by paradoxlost »

MrProgrammer wrote:Hi, and welcome to the forum.
paradoxlost wrote:Basically I need to conde see down a bunch of directories into files.
Why didn't you say so in the first place? XY Problem

Merge sheets of spreadsheet documents

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.

Im not trying to merge sheets of a spreadsheet document. I'm trying to take all the different files in a directory and open them all up as different sheets in a single LO Calc file.
OpenOffice 4.1.7 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Opening new sheet from file w/ python

Post by Villeroy »

paradoxlost wrote:Im not trying to merge sheets of a spreadsheet document. I'm trying to take all the different files in a directory and open them all up as different sheets in a single LO Calc file.
Then go ahead and do so. The sheet merger code provides 90% of what you need. If the data structure of the sheets to be imported is the same, it would be more appropriate to merge all data into one list.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply