| | import requests |
| | import pandas as pd |
| | import io |
| | from fpdf import FPDF |
| | import json |
| |
|
| | def convert_xlsx_to_pdf(file): |
| | """Converts an XLSX file to a PDF and returns a BytesIO object with a filename.""" |
| | excel_data = pd.ExcelFile(file) |
| | pdf = FPDF() |
| | pdf.set_auto_page_break(auto=True, margin=15) |
| | pdf.add_page() |
| | pdf.set_font("Arial", size=12) |
| | |
| | for sheet_name in excel_data.sheet_names: |
| | pdf.cell(200, 10, txt=f"Sheet: {sheet_name}", ln=True, align='C') |
| | pdf.ln(10) |
| | df = excel_data.parse(sheet_name) |
| | |
| | for i in range(min(10, len(df))): |
| | row_data = " | ".join(str(x) for x in df.iloc[i]) |
| | pdf.multi_cell(0, 10, row_data) |
| | |
| | pdf.ln(5) |
| | |
| | pdf_output = io.BytesIO() |
| | pdf_output.write(pdf.output(dest='S').encode('latin1')) |
| | pdf_output.seek(0) |
| | |
| | |
| | pdf_output.name = file.name.replace(".xlsx", ".pdf") |
| | |
| | return pdf_output |
| |
|
| | def upload_file_to_vectara(file, customer_id, api_key, corpus_key): |
| | """Uploads a file to Vectara API v2.""" |
| | url = f"https://api.vectara.io/v2/corpora/{corpus_key}/upload_file" |
| | headers = { |
| | "customer-id": customer_id, |
| | "x-api-key": api_key, |
| | "Accept": "application/json" |
| | } |
| | |
| | metadata = {"type_file": "excel"} if file.name.endswith('.xlsx') else {} |
| | |
| | if file.name.endswith('.xlsx'): |
| | file = convert_xlsx_to_pdf(file) |
| | |
| | files = { |
| | 'metadata': (None, json.dumps(metadata), 'application/json'), |
| | "file": (file.name, file.getvalue())} |
| | |
| | |
| | response = requests.post(url, headers=headers, files=files) |
| | |
| | return response.json() |
| |
|