Package: ez-pdf-tables
ez_pdf_tablesQuickly make PDF tables.Requirements and Supported EnvironmentsPython 3.7+ and Windows or Linux (tested on Ubuntu 20.04.3).This package is dependent on the following Pythjon packages.reportlab3.5+pandas1.3+InstallationInstall via pypi.pip install ez-pdf-tablesUsageTo create a table, an existing dataset is needed. This can be a path to a CSV file, a pandasDataFrameobject, or a list of lists.Provide this along with a destination file and title when instantiating theStandardTableobject.fromez_pdf_tablesimportStandardTablet=StandardTable(r'C:\some\dataset.csv',r'C:\some\destination.pdf','My Report')This creates the object, but not the PDF itself. To finish creation, call thegetfunction.report_pdf=t.get()StandardTablealso has an optionaldefault_leadingsparameter, which is set toTrueby default.
This option sets theleadingattribute for all definedez_pdf_tablesstyles inALL_CUSTOM_STYLESto automatically account for the font size.Any styles within or added toALL_CUSTOM_STYLESlist will be updated whenanyStandardTableis instantiated withdefault_leadingsset toTrueand/orStandardTable.getis called withupdate_leadingsset toTrue.CustomizationYou can optionally set numerous attributes that make table manipulation simple.
A list of all of the possible attributes is in the Table Attributes section below.fromez_pdf_tablesimportStandardTablet=StandardTable('/some/dataset.csv','/some/destination.pdf','Another Report',subtitle_text='A Subtitle',borderless=True)t.get()Alternatively, all attributes can be set after instantiation. The below produces the same result as the above.fromez_pdf_tablesimportStandardTablet=StandardTable('/some/dataset.csv','/some/destination.pdf','Another Report',)t.subtitle_text='A Subtitle',t.borderless=Truet.get()Be AwareThere are a few options that may conflict with one another, or override each other.
For instance,table_style_overridewill ignoreborderless, since it is expected the user
will specify exactly their customreportlab.lib.styles.ParagraphStyleobject.To DoDocumentation.Ambiguous variable names - some variables are named poorly.AllStandardTableAttributesThis section explains all the possible attributes that can manipulate a table's appearance, and their default values.Text to appear below the title. By default, both are not drawn. Ifsubsubtitle_textis supplied andsubtitle_textis not, thesubsubtitle_textwill be drawn directly below the title and a warning issued.subtitle_text = ''subsubtitle_text = ''The default cell and header styles.cell_style = NORMAL_STYLEheader_style = HEADING_STYLEDefault page sizes.A4LETTERis equal to(913.92, 666.96).page_size = A4LETTERpage_width = self.page_size[0]page_margin = HALF_INCHright_page_margin = HALF_INCHleft_page_margin = HALF_INCHtop_page_margin = HALF_INCH / 2bottom_page_margin = HALF_INCH / 4How large the table should be in contrast to canvas.table_scale = 1Default cell size and font size.table_cell_size = 10cell_fontsize = 18Which rows to repeat on each page. If changed to and empty list, none will repeat.repeat_rows = [1,]Default header colour, which isreportlab.lib.colors.cyan.header_color = colors.cyanDraw the table borders or not.borderless = FalseDefine a list of column indices to omit certain columns when drawing.omit_column_list = []Example:t.omit_column_list = [3,9,14]Define a list of column indices to set the column order, will also drop any omitted columnsorder_column_list = []Example:t.order_column_list = [0,1,2,5,3]will rearrange the dataset to put column 5 after column 2 and omit column 4.The length of text a column must reach to be automatically text-wrapped. It is not recommended to set this very high.wrap_limit = 30Define a list of column indices to wrap text in.wrap_column_list = []Define a list of criteria to check for in each cell to see if that entire row should be bold, and/or define a column index where an entire row should be bolded after. Note that if bothbold_criteriaandbold_criteria_indexare specified, both must be met.bold_criteria = []Example:t.bold_criteria = ['Total', 'Count']will bold cells that match either "Total" or "Count".bold_criteria_index = []Example:t.bold_criteria_index = [3,]will bold any cell that is within the target column, 3.Bold the entire row if a criteria is met and is in this list. Note this list can contain criteria without needing to be in bold_criteria.bold_row_criteria = []Define a manual table style. Do not pass areportlab.platypus.TableStyleobject here! Pass the parameters to be passed to the object. See theReportLab documentation (section 7.2, page 85)for information on creating a manual table style.table_style_override = []Define a manual heading style. Must be areportlab.lib.styles.ParagraphStyleobject.override_heading_style = NoneSet to true to disable wrapping of cell values inreportlab.platypus.Paragraph. Also prevents anyreportlab.lib.styles.ParagraphStylefrom being applied to cells.bypass_table_paragraphs = FalseColumns to convert to text when generating. Note that this shouldn't ever be needed aspandas.DataFrameandlistare both converted to text automatically during processing.columns_as_text = []StylesAll included styles inez_pdf_tablesare added to theALL_CUSTOM_STYLESlist constant.NORMAL_STYLEisreportlab.lib.styles.getSampleStyleSheet()['Normal'].HEADING_STYLEisreportlab.lib.styles.getSampleStyleSheet()['Heading5'].TITLE_STYLEis based onreportlab.lib.styles.getSampleStyleSheet()['Title'], with the following modifications:Center alignmentFont size of 48Font type of HelveticaSUBTITLE_STYLEis based onreportlab.lib.styles.getSampleStyleSheet()['Title'], with the following modifications:Font size of 22Space after text of 6BOLD_STYLEis the same asNORMAL_STYLEbut with a font type of Helvetica-Bold.CENTERED_STYLEis the same asNORMAL_STYLEbut with center alignment.SMALL_STYLEis the same asNORMAL_STYLEbut with a font size of 6.BLUE_HIGHLIGHT_STYLE,YELLOW_HIGHLIGHT_STYLE, andGREEN_HIGHLIGHT_STYLEare all the same asNORMAL_STYLEbut with back color ofreportlab.lib.colors.PCMYKColor(25,0,0,0)(blue),reportlab.lib.colors.PCMYKColor(0,0,33,0)(yellow), andreportlab.lib.colors.PCMYKColor(25,0,25,0)(red), respectively.They are all usused and left as examples/options.Each setsbackColorinitially to a presetreportlab.lib.colorsoption and then overwrites it. This is left as an example of what can be done.Additional FeaturesWhen creating PDF reports, it can be advantageous to produce a multiindex dataset without repeating the indices.
This package contains a method for just that,multiindex_as_is.Inpandas,  a multiindex can be set on aDataFramevia passing a list of target
indices topandas.DataFrame.set_index. The same logic will apply here.fromez_pdf_tablesimportmultiindex_as_isdf=pd.read_csv(r'C:\some\dataset.csv')df=multiindex_as_is(df,['List','Of','Indices'])Using this method can produce some pleasant tables. See examples for a full example.ExamplesMake a Default TableNo frills, default options.fromez_pdf_tablesimportStandardTablet=StandardTable('/home/username/Desktop/salaries.csv','/home/username/Desktop/salaries.pdf','Employee Salaries')t.get()Borderless and SubtitlesSetting the table to be borderless and adding a subtitle.t=StandardTable('/home/username/Desktop/salaries.csv','/home/username/Desktop/salaries.pdf','Employee Salaries',borderless=True,subtitle_text='As of 05/20/2020')t.get()Using BoldSpecifying values in thebold_criteriaparameter is a simple way to apply bold selectively.t=StandardTable(r"C:\some\dataset.csv",r"C:\some\bold.pdf",'Team Scores',bold_criteria=['Yes',])t.get()However, it can be problematic if values are repeated in multiple columns.In this case, usingbold_criteria_indexwithbold_criteriacan narrow the search.t=StandardTable(r"C:\some\dataset.csv",r"C:\some\bold.pdf",'Team Scores',bold_criteria=['Yes',],bold_criteria_index=[2,])t.get()Of course, if an entire column should always be bold, usingbold_criteria_indexalone works, too.t=StandardTable(r"C:\some\dataset.csv",r"C:\some\bold.pdf",'Team Scores',bold_criteria_index=[2,])t.get()Sometimes it may be desirable to embolden the entire row if the row contains some criteria.
This can be defined similar to the above, by cell contents, inbold_row_criteria.t=StandardTable(r"C:\some\dataset.csv",r"C:\some\bold.pdf",'Team Scores',bold_row_criteria=[15,])t.get()Combining all of these is possible, but likely unnecessary.t=StandardTable(r"C:\some\dataset.csv",r"C:\some\bold.pdf",'Team Scores',bold_criteria=['Yes',],bold_criteria_index=[2,],bold_row_criteria=[15,])t.get()Multiindex TablesMultiindex allows tables to have sorted and/or hanging values. Pass a CSV file orpandas.DataFrameobject into themultiindex_as_isfunction, along with a list of what indices should be used, in the correct order.fromez_pdf_tablesimportmultiindex_as_is,StandardTablemi=multiindex_as_is(r"C:\some\dataset.csv",['Team','Score'])t=StandardTable(mi,r"C:\some\mi.pdf",'Team Scores')t.borderless=True# These look great borderlesst.get()