A collection of useful Python scripts for various tasks.
src/python_utils/image/convert_pdf_image.py: Converts PDF files to PNG images. It uses PyMuPDF to render PDF pages into high-resolution images (300 DPI). The script preserves the directory structure of the source PDFs.
src/python_utils/pdf/convert_docs_to_pdf.py: Converts.docand.docxfiles to PDF format using LibreOffice. It recursively searches for documents in a given directory and converts them in place.
src/python_utils/json2excel/json_to_excel.py: Converts JSON files with nested structures to Excel spreadsheets with a flattened tabular format. Supports both single file and batch directory conversion.
src/python_utils/pydanticai/init_google_agent.py: Initializes a Pydantic AI agent with a Google language model (e.g., Gemini). It handles loading Google Cloud credentials and configuring the agent with specific model settings.
- Python 3.12+
- LibreOffice needs to be installed and in your system's PATH for the
.doc/.docxto PDF conversion. - Google Cloud credentials for the Pydantic AI agent.
-
Clone the repository:
git clone https://github.com/mfmezger/python_utils.git cd python_utils -
Install dependencies: This project uses
uvfor package management.pip install uv uv sync
-
Set up pre-commit hooks: This project uses
pre-committo enforce code quality.pre-commit install
After installation, use the python-utils CLI:
# Convert JSON to Excel
python-utils json2excel input.json -o output.xlsx
python-utils json2excel ./json_directory/ -o ./excel_output/
# Convert PDFs to images
python-utils pdf2image ./pdfs/ ./images/
# Convert .doc/.docx to PDF
python-utils doc2pdf ./documents/
# Show help
python-utils --helpImport modules directly in your code:
# Image conversion
from python_utils.image import convert_pdfs
convert_pdfs(src_root="./pdfs", dest_root="./images")
# JSON to Excel
from python_utils.json2excel import convert_json_to_excel
convert_json_to_excel(Path("data.json"), Path("output.xlsx"))
# Document to PDF
from python_utils.pdf import convert_docs_to_pdf
convert_docs_to_pdf("./documents")
# Pydantic AI Agent
from python_utils.pydanticai import initialize_agent
agent = initialize_agent(
prompt="You are a helpful assistant.",
output_model=MyOutputModel,
)MIT License - see LICENSE for details.