Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Al-Muhandis
Last active December 18, 2023 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Al-Muhandis/e9edd46c861622e547d86320174b8e88 to your computer and use it in GitHub Desktop.
Save Al-Muhandis/e9edd46c861622e547d86320174b8e88 to your computer and use it in GitHub Desktop.
Example demonstrating filling out the odt documents files using the native FPC paszlib library
unit zip_odt;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils;
procedure FillODTDoc(const aSrcFile, aDestFile: String; aKeyValuePairs: TStringList; const aODTSubFile: String = '');
implementation
uses
Zipper, FileUtil
;
procedure UnzipODT(const aUnzipDir, aZipFile: String);
var
aUnZipper: TUnZipper;
begin
aUnZipper:=TUnZipper.Create;
try
ForceDirectories(aUnzipDir);
aUnZipper.OutputPath:=aUnzipDir;
aUnZipper.UnZipAllFiles(aZipFile);
finally
aUnZipper.Free;
end;
end;
procedure ZipODT(const aSrcDir, aZipFile: String);
var
aZipper: TZipper;
aList: tStringList;
s: String;
begin
aZipper := TZipper.create;
aList := TStringList.create;
try
FindAllFiles(aList, aSrcDir);
aZipper.FileName := aZipFile;
for s in aList do
aZipper.Entries.AddFileEntry(s, ExtractRelativePath(aSrcDir, s));
aZipper.ZipAllFiles;
finally
aZipper.Free;
aList.free;
end;
end;
procedure FillContent(const aPath: String; const aKey, aValue: String; const aODTSubFile: String = '');
var
S, aFile: String;
aContent: TStringList;
begin
if aODTSubFile.IsEmpty then
aFile:=IncludeTrailingPathDelimiter(aPath)+'content.xml'
else
aFile:=IncludeTrailingPathDelimiter(aPath)+aODTSubFile;
S:=ReadFileToString(aFile);
S:=StringReplace(S, aKey, aValue, [rfReplaceAll]);
aContent:=TStringList.Create;
try
aContent.Text:=S;
aContent.SaveToFile(aFile);
finally
aContent.Free;
end;
end;
procedure FillContent(const aPath: String; aKeyValuePairs: TStringList; const aODTSubFile: String = '');
var
S, aFile: String;
aContent: TStringList;
i: Integer;
begin
if aODTSubFile.IsEmpty then
aFile:=IncludeTrailingPathDelimiter(aPath)+'content.xml'
else
aFile:=IncludeTrailingPathDelimiter(aPath)+aODTSubFile;
S:=ReadFileToString(aFile);
for i:=0 to aKeyValuePairs.Count-1 do
S:=StringReplace(S, aKeyValuePairs.Names[i], aKeyValuePairs.ValueFromIndex[i], [rfReplaceAll]);
aContent:=TStringList.Create;
try
aContent.Text:=S;
aContent.SaveToFile(aFile);
finally
aContent.Free;
end;
end;
{ aODTSubFile - a file to be processed in addition to the main content.xml }
procedure FillODTDoc(const aSrcFile, aDestFile: String; aKeyValuePairs: TStringList; const aODTSubFile: String = '');
var
aTempPath: String;
begin
aTempPath:=IncludeTrailingPathDelimiter(GetTempDir(False))+'zipodt'+PathDelim;
ForceDirectories(aTempPath);
UnzipODT(aTempPath, aSrcFile);
FillContent(aTempPath, aKeyValuePairs);
if not aODTSubFile.IsEmpty then
FillContent(aTempPath, aKeyValuePairs, aODTSubFile);
ZipODT(aTempPath, aDestFile);
DeleteDirectory(aTempPath, True);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment