PDFHelper Module

Provides utilities for handling, reading, and writing PDF files.

HMB.PDFHelper.ReadFullPDF(filePath)[source]

Reads the full content of a PDF file and returns it as a string.

Parameters:

filePath (str) – The path to the PDF file to be read.

Returns:

The full text content of the PDF file.

Return type:

str

HMB.PDFHelper.ReadPDFPage(filePath, pageNum)[source]

Reads the content of a specific page in a PDF file.

Parameters:
  • filePath (str) – The path to the PDF file.

  • pageNum (int) – The page number to read (0-based).

Returns:

The text content of the specified page.

Return type:

str

HMB.PDFHelper.GetPDFPageCount(filePath)[source]

Returns the total number of pages in a PDF file.

Parameters:

filePath (str) – The path to the PDF file.

Returns:

The number of pages in the PDF.

Return type:

int

HMB.PDFHelper.ExtractPDFTextByRegex(filePath, pattern)[source]

Extracts all text matching a regex pattern from a PDF file.

Parameters:
  • filePath (str) – The path to the PDF file.

  • pattern (str) – The regex pattern to search for.

Returns:

A list of matched strings.

Return type:

list

HMB.PDFHelper.ExtractPDFMetadata(filePath)[source]

Extracts metadata from a PDF file.

Parameters:

filePath (str) – The path to the PDF file.

Returns:

Metadata dictionary.

Return type:

dict

HMB.PDFHelper.ExtractPDFImages(filePath)[source]

Extracts images from all pages of a PDF file.

Parameters:

filePath (str) – The path to the PDF file.

Returns:

A list of image bytes objects.

Return type:

list

HMB.PDFHelper.ExtractPDFTables(filePath, pages='all')[source]

Extracts tables from a PDF file using tabula-py.

Installation:

Use pip install tabula-py to install the library. Use pip install jpype1 to install the JPype1 dependency. Also requires Java to be installed on the system. Install it from https://www.oracle.com/java/technologies/downloads/

Parameters:
  • filePath (str) – The path to the PDF file.

  • pages (str or int) – Pages to extract tables from.

Returns:

List of pandas DataFrames for each table.

Return type:

list

HMB.PDFHelper.SplitPDF(filePath, startPage, endPage, outputPath)[source]

Saves a range of pages from a PDF file as a new PDF.

Parameters:
  • filePath (str) – The path to the PDF file.

  • startPage (int) – The starting page (0-based).

  • endPage (int) – The ending page (exclusive, 0-based).

  • outputPath (str) – The path to save the new PDF.

HMB.PDFHelper.MergePDFs(pdfPaths, outputPath)[source]

Merges multiple PDF files into a single PDF.

Parameters:
  • pdfPaths (list) – List of PDF file paths to merge.

  • outputPath (str) – Path to save the merged PDF.

HMB.PDFHelper.DeletePDFPages(filePath, pagesToDelete, outputPath)[source]

Deletes specified pages from a PDF and saves the result.

Parameters:
  • filePath (str) – Path to the PDF file.

  • pagesToDelete (list) – List of 0-based page indices to delete.

  • outputPath (str) – Path to save the new PDF.

HMB.PDFHelper.RotatePDFPages(filePath, rotation, outputPath, pages=None)[source]

Rotates specified pages in a PDF by a given angle.

Parameters:
  • filePath (str) – Path to the PDF file.

  • rotation (int) – Angle to rotate (90, 180, 270).

  • outputPath (str) – Path to save the rotated PDF.

  • pages (list or None) – List of 0-based page indices to rotate. If None, rotate all.

HMB.PDFHelper.ExtractPDFAnnotations(filePath)[source]

Extracts annotations/comments from all pages of a PDF file.

Parameters:

filePath (str) – Path to the PDF file.

Returns:

List of annotation texts.

Return type:

list

HMB.PDFHelper.PDFContainsText(filePath, searchText, caseSensitive=False, useRegex=False, returnPositions=False)[source]

Checks if the PDF contains the specified text, with options for case sensitivity, regex, and returning positions.

Parameters:
  • filePath (str) – Path to the PDF file.

  • searchText (str) – Text or regex pattern to search for.

  • caseSensitive (bool) – If True, search is case sensitive.

  • useRegex (bool) – If True, searchText is treated as a regex pattern.

  • returnPositions (bool) – If True, returns list of (pageNum, position) for all occurrences.

Returns:

True/False if found (default), or list of (pageNum, position) if returnPositions is True.

Return type:

bool or list

HMB.PDFHelper.AddPDFBookmark(filePath, pageNum, title, outputPath)[source]

Adds a bookmark to a specific page in the PDF.

Parameters:
  • filePath (str) – Path to the PDF file.

  • pageNum (int) – Page number (0-based).

  • title (str) – Bookmark title.

  • outputPath (str) – Path to save the new PDF.

Extracts all hyperlinks from the PDF.

Parameters:

filePath (str) – Path to the PDF file.

Returns:

List of URLs.

Return type:

list

HMB.PDFHelper.HighlightPDFText(filePath, searchText, outputPath, color=(1, 1, 0))[source]

Highlights all occurrences of a text string in the PDF.

Parameters:
  • filePath (str) – Path to the PDF file.

  • searchText (str) – Text to highlight.

  • outputPath (str) – Path to save the highlighted PDF.

  • color (tuple) – RGB color tuple for highlight (default yellow).

Returns:

True if any text was highlighted, False otherwise.

Return type:

bool

HMB.PDFHelper.EncryptPDF(filePath, password, outputPath)[source]

Encrypts the PDF with a password.

Parameters:
  • filePath (str) – Path to the PDF file.

  • password (str) – Password to set.

  • outputPath (str) – Path to save the encrypted PDF.

HMB.PDFHelper.DecryptPDF(filePath, password, outputPath)[source]

Removes password protection from a PDF.

Parameters:
  • filePath (str) – Path to the encrypted PDF.

  • password (str) – Password for decryption.

  • outputPath (str) – Path to save the decrypted PDF.

HMB.PDFHelper.ExtractPDFFonts(filePath)[source]

Lists all fonts used in the PDF.

Parameters:

filePath (str) – Path to the PDF file.

Returns:

List of font names.

Return type:

list