Last active
October 31, 2024 20:34
-
-
Save luismendes070/ac8a86a1cccb8adaf10a9784d0aaa694 to your computer and use it in GitHub Desktop.
convert csv to pdf ChatGPT NotebookLM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from fpdf import FPDF | |
# Configurações | |
csv_file = 'seu_arquivo.csv' # Substitua pelo nome do seu arquivo CSV | |
pdf_file = 'saida.pdf' # Nome do arquivo PDF de saída | |
chunk_size = 50 # Quantidade de linhas por chunk (pode ser ajustado) | |
# Inicializar o PDF | |
pdf = FPDF() | |
pdf.set_auto_page_break(auto=True, margin=15) | |
pdf.add_page() | |
pdf.set_font("Arial", size=10) | |
# Função para adicionar um chunk de dados ao PDF | |
def add_chunk_to_pdf(pdf, chunk): | |
# Escreve cabeçalhos | |
col_widths = [40] * len(chunk.columns) # Ajuste conforme necessário | |
pdf.set_fill_color(200, 220, 255) | |
for col_name in chunk.columns: | |
pdf.cell(col_widths[0], 10, col_name, border=1, fill=True) | |
pdf.ln() | |
# Adiciona linhas de dados | |
for _, row in chunk.iterrows(): | |
for idx, value in enumerate(row): | |
pdf.cell(col_widths[idx], 10, str(value), border=1) | |
pdf.ln() | |
# Processar o CSV em pedaços | |
try: | |
print(f"Lendo o arquivo: {csv_file}") | |
for chunk in pd.read_csv(csv_file, chunksize=chunk_size, encoding='latin1'): | |
add_chunk_to_pdf(pdf, chunk) | |
pdf.add_page() | |
pdf.output(pdf_file) | |
print(f"Arquivo PDF salvo como: {pdf_file}") | |
except Exception as e: | |
print(f"Erro ao processar o arquivo: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment