Package: libzim
python-libzimlibzimmodule allows you to read and writeZIM
filesin Python. It provides a shallow python
interface on top of theC++libzimlibrary.It is primarily used inopenZIMscrapers likesotokioryoutube2zim.InstallationpipinstalllibzimOurPyPI wheelsbundle arecent releaseof the C++ libzim and are available for the following platforms:macOS forx86_64andarm64GNU/Linux forx86_64,armhfandaarch64Linux+musl forx86_64andaarch64Wheels are available for both CPython and PyPy.Users on other platforms can install the source distribution (seeBuildingbelow).Contributionsgitclonegit@github.com:openzim/python-libzim.git&&cdpython-libzim# python -m venv env && source env/bin/activatepipinstall-Usetuptoolsinvoke
invokedownload-libziminstall-devbuild-exttest# invoke --list for available development helpersSeeCONTRIBUTING.mdfor additional details thenOpen a ticketor submit a Pull Request on Github 🤗!UsageRead a ZIM filefromlibzim.readerimportArchivefromlibzim.searchimportQuery,Searcherfromlibzim.suggestionimportSuggestionSearcherzim=Archive("test.zim")print(f"Main entry is at{zim.main_entry.get_item().path}")entry=zim.get_entry_by_path("home/fr")print(f"Entry{entry.title}at{entry.path}is{entry.get_item().size}b.")print(bytes(entry.get_item().content).decode("UTF-8"))# searching using full-text indexsearch_string="Welcome"query=Query().set_query(search_string)searcher=Searcher(zim)search=searcher.search(query)search_count=search.getEstimatedMatches()print(f"there are{search_count}matches for{search_string}")print(list(search.getResults(0,search_count)))# accessing suggestionssearch_string="kiwix"suggestion_searcher=SuggestionSearcher(zim)suggestion=suggestion_searcher.suggest(search_string)suggestion_count=suggestion.getEstimatedMatches()print(f"there are{suggestion_count}matches for{search_string}")print(list(suggestion.getResults(0,suggestion_count)))Write a ZIM filefromlibzim.writerimportCreator,Item,StringProvider,FileProvider,HintclassMyItem(Item):def__init__(self,title,path,content="",fpath=None):super().__init__()self.path=pathself.title=titleself.content=contentself.fpath=fpathdefget_path(self):returnself.pathdefget_title(self):returnself.titledefget_mimetype(self):return"text/html"defget_contentprovider(self):ifself.fpathisnotNone:returnFileProvider(self.fpath)returnStringProvider(self.content)defget_hints(self):return{Hint.FRONT_ARTICLE:True}content="""<html><head><meta charset="UTF-8"><title>Web Page Title</title></head><body><h1>Welcome to this ZIM</h1><p>Kiwix</p></body></html>"""item=MyItem("Hello Kiwix","home",content)item2=MyItem("Bonjour Kiwix","home/fr",None,"home-fr.html")withCreator("test.zim").config_indexing(True,"eng")ascreator:creator.set_mainpath("home")creator.add_item(item)creator.add_item(item2)forname,valuein{"creator":"python-libzim","description":"Created in python","name":"my-zim","publisher":"You","title":"Test ZIM",}.items():creator.add_metadata(name.title(),value)Buildinglibzimpackage building offers different behaviors via environment variablesVariableExampleUse caseLIBZIM_DL_VERSION8.1.1or2023-04-14Specify the C++ libzim binary version to download and bundle. Either a release version string or a date, in which case it downloads a nightlyUSE_SYSTEM_LIBZIM1UsesLDFLAGandCFLAGSto find the libzim to link against. Resulting wheel won't bundle C++ libzim.DONT_DOWNLOAD_LIBZIM1Disable downloading of C++ libzim. Place headers ininclude/and libzim dylib/so inlibzim/if no using system libzim. It will be bundled in wheel.PROFILE1Enable profile tracing in Cython extension. Required for Cython code coverage reporting.SIGN_APPLE1Set to sign and notarize the extension for macOS. Requires following informationsAPPLE_SIGNING_IDENTITYDeveloper ID Application: OrgName (ID)Required for signing on macOSAPPLE_SIGNING_KEYCHAIN_PATH/tmp/build.keychainPath to the Keychain containing the certificate to sign for macOS withAPPLE_SIGNING_KEYCHAIN_PROFILEbuildName of the profile in the specified KeychainExamplesDefault: downloading and bundling most appropriate libzim release binarypython3-mbuildUsing system libzim (brew, debian or manually installed) - not bundled# using system-installed C++ libzimbrewinstalllibzim# macOSapt-getinstalllibzim-devel# debiandnfinstalllibzim-dev# fedoraUSE_SYSTEM_LIBZIM=1python3-mbuild--wheel# using a specific C++ libzimUSE_SYSTEM_LIBZIM=1\CFLAGS="-I/usr/local/include"\LDFLAGS="-L/usr/local/lib"DYLD_LIBRARY_PATH="/usr/local/lib"\LD_LIBRARY_PATH="/usr/local/lib"\python3-mbuild--wheelOther platformsOn platforms for which there is noofficial binaryavailable, you'd have tocompile C++ libzim from sourcefirst then either useDONT_DOWNLOAD_LIBZIMorUSE_SYSTEM_LIBZIM.LicenseGPLv3or later, seeLICENSEfor more details.