mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 15:48:29 +00:00
Merge pull request #174 from crewAIInc/extra_deps
add optional dependencies for all deps that we have
This commit is contained in:
@@ -1,29 +1,50 @@
|
||||
from typing import Any, Optional, Type
|
||||
from typing import Optional, Type
|
||||
from pydantic import BaseModel, Field
|
||||
from pypdf import PdfReader, PdfWriter, PageObject, ContentStream, NameObject, Font
|
||||
from pathlib import Path
|
||||
from crewai_tools.tools.rag.rag_tool import RagTool
|
||||
|
||||
|
||||
class PDFTextWritingToolSchema(BaseModel):
|
||||
"""Input schema for PDFTextWritingTool."""
|
||||
|
||||
pdf_path: str = Field(..., description="Path to the PDF file to modify")
|
||||
text: str = Field(..., description="Text to add to the PDF")
|
||||
position: tuple = Field(..., description="Tuple of (x, y) coordinates for text placement")
|
||||
position: tuple = Field(
|
||||
..., description="Tuple of (x, y) coordinates for text placement"
|
||||
)
|
||||
font_size: int = Field(default=12, description="Font size of the text")
|
||||
font_color: str = Field(default="0 0 0 rg", description="RGB color code for the text")
|
||||
font_name: Optional[str] = Field(default="F1", description="Font name for standard fonts")
|
||||
font_file: Optional[str] = Field(None, description="Path to a .ttf font file for custom font usage")
|
||||
font_color: str = Field(
|
||||
default="0 0 0 rg", description="RGB color code for the text"
|
||||
)
|
||||
font_name: Optional[str] = Field(
|
||||
default="F1", description="Font name for standard fonts"
|
||||
)
|
||||
font_file: Optional[str] = Field(
|
||||
None, description="Path to a .ttf font file for custom font usage"
|
||||
)
|
||||
page_number: int = Field(default=0, description="Page number to add text to")
|
||||
|
||||
|
||||
class PDFTextWritingTool(RagTool):
|
||||
"""A tool to add text to specific positions in a PDF, with custom font support."""
|
||||
|
||||
name: str = "PDF Text Writing Tool"
|
||||
description: str = "A tool that can write text to a specific position in a PDF document, with optional custom font embedding."
|
||||
args_schema: Type[BaseModel] = PDFTextWritingToolSchema
|
||||
|
||||
def run(self, pdf_path: str, text: str, position: tuple, font_size: int, font_color: str,
|
||||
font_name: str = "F1", font_file: Optional[str] = None, page_number: int = 0, **kwargs) -> str:
|
||||
def run(
|
||||
self,
|
||||
pdf_path: str,
|
||||
text: str,
|
||||
position: tuple,
|
||||
font_size: int,
|
||||
font_color: str,
|
||||
font_name: str = "F1",
|
||||
font_file: Optional[str] = None,
|
||||
page_number: int = 0,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
reader = PdfReader(pdf_path)
|
||||
writer = PdfWriter()
|
||||
|
||||
@@ -63,4 +84,4 @@ class PDFTextWritingTool(RagTool):
|
||||
with open(font_file, "rb") as file:
|
||||
font = Font.true_type(file.read())
|
||||
font_ref = writer.add_object(font)
|
||||
return font_ref
|
||||
return font_ref
|
||||
|
||||
Reference in New Issue
Block a user