Package: confectionary
PDF Confectionary :cupcake:Work in the NLP domain and find that your end users/clients don't like using.txtfiles with your excellent results? Look no further!PDF Confectionary is a tool for quickly creating templated PDFs from text files usingFPDF2. Essentially, point it at a directory of text files, and generate somesweetPDFs.Quickly convert text files into readable, paragraph-segmented PDFs that are easy to navigate.Table of ContentsAboutInstallationRequirementsPackage InstallationUsageCommand Line UsageBasic Usage within PythonNote: word2vec model loadingTODO listLicenseContributingAboutThe focus of this repo is to provide a simple, easy-to-use, and extensible PDF creation tool. Relevant features in PDF Confectionary include:Automatic paragraph separation via thetextsplitmoduleFast navigation through generated PDFs via links between TOC to chapters & footer links back to TOC.Keyword extraction for each text fileThis module was inspired by the need to create clean output documents for reading & review speech transcription from thevid2cleantxtproject. PDF Confectionary was initially designed as a command-line tool but provides a Python API for more advanced use cases.InstallationRequirementsPrimary modules used byconfectionaryare:FPDF2,textsplit,gensim, andclean-text.All dependencies are listed in therequirements.txtfile.Package InstallationThe package can be installed using pip:pipinstallconfectionaryTo install as a python package without pip, run:gitclone<https://github.com/pszemraj/confectionary.git>cdconfectionary
pipinstall-e.UsageThere are two ways to use PDF Confectionary:command line, viapython confectionary/text2pdf.py -i <input_dir> -o <output_dir>Python API via functions in theconfectionary.text2pdfmodule. Thedir_to_pdffunction is the equivalent of the command line tool application.Both create one pdf from all txt files in the input directory, saved tooutput_dir. Add the-rswitch (orrecurse=Truein function) to load files recursively.Command Line UsageCallpython confectionary/text2pdf.py -i /path/to/input/dir -o /path/to/output/dirto create a pdf from all txt files in the input directory and save it to the output directory:pythonconfectionary/text2pdf.py-i/path/to/input/dir-o/path/to/output/dir\-kw"my keywords to label this document."The below example shows the output of the command line tool and uses the-mswitch to specify a specific word2vec model.$pythonconfectionary/text2pdf.py-i"example/text-files"-o"example/outputs"-kw"my keywords to label this document"\-m"glove-wiki-gigaword-200"Output:Since the GPL-licensed package `unidecode` is not installed, using Pythons `unicodedata` package which yields worse results.
3 files found matching extension .txt

# entries is 3, < title thresh 39
will use one page for TOC

Building Chapters in PDF file:   0%|                                                                                    | 0/3 [00:00<?, ?it/s]
No local model file - downloading glove-wiki-gigaword-200 from gensim-data API
[==================================================] 100.0% 252.1/252.1MB downloaded

Loaded word2vec model glove-wiki-gigaword-200
Building Chapters in PDF file: 100%|████████████████████████████████████████████████████████████████████████████| 3/3 [01:23<00:00, 27.77s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 3484.61it/s]

PDF file written to example/outputs/text-to-PDF/my keywords to label this document_txt2pdf_Oct-18-2022_standard.pdfFind out more about the command line tool by runningpython confectionary/text2pdf.py -h.Basic Usage within PythonThree basic functions are available inconfectionary.text2pdf:dir_to_pdf,file_to_pdf, andstr_to_pdf:dir_to_pdftakes a directory path and creates a pdf from all txt files in the directory.file_to_pdftakes a file path and creates a pdf from the file.str_to_pdftakes a string and creates a pdf from the string.Details on the function arguments can be found in the relevant function docstrings (or callhelp()). To replicate the above command line usage, use the following code:fromconfectionary.text2pdfimportdir_to_pdfreport_path=dir_to_pdf(input_dir="/path/to/input/dir",output_dir="/path/to/output/dir",keywords="my keywords to label this document",)print(f"Report saved to{report_path}")Check out thedir_to_pdfdocstring for more options:importinspectfromconfectionary.text2pdfimportdir_to_pdfinspect.getdoc(dir_to_pdf)Note: word2vec model loadingSplitting input text into paragraphs is enabled by default and uses aword2vecmodel. If it doesn't exist, it will be downloaded viagensim's API and saved to the./modelsdirectory.the quality of the paragraph splitting and runtime of the script both depend on the size and complexity of theword2vecmodel. If you want to use a different model, you can pass the path to the model to thedir_to_pdffunction via theword2vec_modelargument.the default model isglove-wiki-gigaword-100and is a 100-dimensional model and has a download size of ~130 MB.additional models that are downloadable are listedhere. This info is also available by passing the--api-infoflag to the command line tool or calling theconfectionary.utils.print_api_info()function.Using paragraph splitting is not required and can be disabled by setting thedo_paragraph_splittingparameter toFalseor, in command line mode, by adding the--no-splitswitch.TODO listconvert thetext2pdf.pyscript to a module/functionpublish to PyPIadd alternate, smaller,word2vecmodels for splitting paragraphsimprove TOC calculation beyond a simple title thresholdAdd a basic notebook demoLicenseApache License 2.0ContributingGiven the open-ended nature of documentation creation, there are a lot of features that are not yet implemented. Please feel free to contribute!Developers can contribute to this project by submitting pull requests in this repo - see details in thecontributing guide.