I have a calc document with 100 sheets. I want to generate 100 pdf with the name of each sheet, not one big pdf…
I managed to do it in Excel but not in Libre Office…
I managed to do it in Excel
How? Trying to export to PDF from Excel 2016, I only see an option to export to a single file (maybe limiting to active sheet or selection), not to a series of files…
Also: A sheetname may not be qualified as a filename.
If the most simple case concerning the names and the target folder (same as origional .ods) as well as the pdf options is assured, a macro for the task shouldn’t be too complicated. Study the famous texts by Andrew Pitonyak. The appropriate export filter should be calc_pdf_Export
.
See also (e.g.): [Solved] Macro to export to PDF [code added for] save as ods (View topic) • Apache OpenOffice Community Forum.
How did you “do it in Excel”?
It is not very difficult
Sub ExportAllSheetsToPDF
Rem Export options. Now they are minimal. To adjust the PDF format accurately,
Rem see https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export
Dim ExportArgs(1) as new com.sun.star.beans.PropertyValue
Dim dataRange(0) as new com.sun.star.beans.PropertyValue
Dim oSheets As Variant, oSheet As Variant, oCursor As Variant
Dim sPath As String, sFileName As String
Dim i As Long
GlobalScope.BasicLibraries.LoadLibrary("Tools")
sFileName = ThisComponent.URL
If sFileName = "" Then Exit Sub ' This spreadsheet wasn't saved, so it hasn't path and name '
Rem Folder to store PDF - same as source file
sPath = ConvertFromURL(DirectoryNameoutofPath(sFileName,"/"))
If Right(sPath,1) <> GetPathSeparator() Then sPath = sPath + GetPathSeparator()
ExportArgs(0).Name = "FilterName"
ExportArgs(0).Value = "calc_pdf_Export"
ExportArgs(1).Name = "FilterData"
dataRange(0).Name = "Selection"
oSheets = ThisComponent.getSheets()
For i = 0 To oSheets.getCount()-1
oSheet = oSheets.getByIndex(i)
If oSheet.isVisible Then ' Skip hidden sheets '
Rem Export range with data only (skip first empty columns and rows)
oCursor = oSheet.createCursor()
oCursor.gotoStartOfUsedArea(False)
oCursor.gotoEndOfUsedArea(True)
Rem Set range with data as param of filter export
dataRange(0).Value = oCursor
ExportArgs(1).Value = dataRange()
Rem Create filename by sheet name
sFileName = sPath + oSheet.getName()+".pdf"
Rem Export
ThisComponent.StoreToURL(ConvertToURL(sFileName),ExportArgs())
EndIf
Next i
End Sub
how do I exports all sheets to single
pdf files with sheet name?
You can do it by your own macro.
I took the code from the last answer and altered a little bit. The macro now asks for a folder to put the files in and asks for the sheets that should be exported as PDF files. Sheet names are used as filenames. There’s no check whether the sheetnames qualify for filenames. If something goes wrong, rename your sheets. (Note: Files already existing in output folder will be overwritten.)
REM ***** BASIC *****
Sub ExportSelectedSheetsToPDF
Dim ExportArgs(1) As New com.sun.star.beans.PropertyValue
Dim oSheets As Variant, oSheet As Variant
Dim sPath As String, sFileName As String
Dim i As Long
Dim selectedSheets As Object
Dim sheetName As String
Dim count As Long
Dim userInput As String
Dim sheetIndex As Variant
Dim sheetsArray() As Integer
' Speicherort abfragen
Dim fileDialog As Object
fileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
fileDialog.setTitle("Speicherort auswählen")
fileDialog.setDisplayDirectory("file:///home/")
fileDialog.setSelectFile("Speichern")
fileDialog.setMultiSelectionMode(False)
If fileDialog.execute() = 1 Then
sPath = fileDialog.getSelectedFiles()(0)
Else
MsgBox "Abgebrochen."
Exit Sub
End If
' Abfrage nach den Tabellenblattnummern
userInput = InputBox("Geben Sie die Blattnummern an (z.B. 1, 2, 4-6):", "Tabellenblätter auswählen")
' Zahlen und Bereiche in ein Array umwandeln
sheetsArray = ConvertInputToSheetIndices(userInput)
' Export-Argumente einstellen
ExportArgs(0).Name = "FilterName"
ExportArgs(0).Value = "calc_pdf_Export"
' Auf die Arbeitsblätter zugreifen
oSheets = ThisComponent.getSheets()
count = 0
For i = LBound(sheetsArray) To UBound(sheetsArray)
If sheetsArray(i) < oSheets.getCount() Then
oSheet = oSheets.getByIndex(sheetsArray(i))
If oSheet.isVisible Then ' Nur sichtbare Blätter exportieren
' Dateiname erstellen
sheetName = oSheet.getName()
sFileName = sPath & "/" & sheetName & ".pdf"
' Exportieren
ThisComponent.StoreToURL(ConvertToURL(sFileName), ExportArgs())
count = count + 1
End If
End If
Next i
MsgBox count & " PDFs wurden erfolgreich exportiert."
End Sub
Function ConvertInputToSheetIndices(userInput As String) As Integer()
Dim indices() As Integer
Dim i As Integer
Dim parts() As String
Dim j As Integer
Dim start As Integer
Dim finish As Integer
parts = Split(userInput, ",")
For i = LBound(parts) To UBound(parts)
parts(i) = Trim(parts(i))
If InStr(parts(i), "-") > 0 Then
Dim rangeParts() As String
rangeParts = Split(parts(i), "-")
start = CInt(Trim(rangeParts(0))) - 1
finish = CInt(Trim(rangeParts(1))) - 1
For j = start To finish
indices = AppendToArray(indices, j)
Next j
Else
indices = AppendToArray(indices, CInt(parts(i)) - 1)
End If
Next i
ConvertInputToSheetIndices = indices
End Function
Function AppendToArray(arr() As Integer, value As Integer) As Integer()
Dim newSize As Integer
newSize = UBound(arr) + 1 + 1
ReDim Preserve arr(newSize - 1)
arr(newSize - 1) = value
AppendToArray = arr
End Function