Created
April 19, 2025 09:30
-
-
Save xavetar/edc66cc1510bcf09aec270fb920a8094 to your computer and use it in GitHub Desktop.
csv2pdf
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
| #!/usr/bin/env python3 | |
| import os | |
| import argparse | |
| import numpy as np | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from matplotlib.backends.backend_pdf import PdfPages | |
| def csv_to_pdf(csv_file, pdf_file): | |
| # Read CSV file | |
| df = pd.read_csv(csv_file) | |
| # Create a PDF file | |
| with PdfPages(pdf_file) as pdf: | |
| # Calculate figure size based on data | |
| n_rows = len(df) + 1 # +1 for header | |
| n_cols = len(df.columns) | |
| row_height = 0.15 # Approximate height per row in inches (adjusted for font size 8) | |
| fig_height = n_rows * row_height # Precise height based on content | |
| fig_width = min(max(8, n_cols * 0.5), 50) # Keep width calculation | |
| # Create figure | |
| fig, ax = plt.subplots(figsize=(fig_width, fig_height)) | |
| # Remove axes | |
| ax.axis('off') | |
| # Create table with adjusted cell sizes | |
| table = ax.table(cellText=df.values, | |
| colLabels=df.columns, | |
| loc='center', | |
| cellLoc='center') | |
| # Style table | |
| table.auto_set_font_size(False) | |
| table.set_fontsize(8) | |
| # Auto adjust column widths | |
| table.auto_set_column_width(np.arange(len(df.columns))) | |
| # Scale table to fit width, keep height proportional | |
| scale_factor_x = min(fig_width / n_cols * 0.8, 1.5) | |
| scale_factor_y = 1.0 # Avoid vertical stretching to prevent compression | |
| table.scale(scale_factor_x, scale_factor_y) | |
| # Minimize padding to reduce white space | |
| plt.subplots_adjust(left=0.02, right=0.98, top=0.99, bottom=0.01) | |
| # Save to PDF with minimal padding | |
| pdf.savefig(bbox_inches='tight', pad_inches=0.01) # Minimal padding | |
| plt.close() | |
| def main(): | |
| # Set up argument parser | |
| parser = argparse.ArgumentParser( | |
| description="Convert a CSV file to a PDF table." | |
| ) | |
| parser.add_argument( | |
| "csv_file", | |
| type=str, | |
| help="Path to the input CSV file" | |
| ) | |
| parser.add_argument( | |
| "pdf_file", | |
| type=str, | |
| nargs="?", | |
| default=None, | |
| help="Path to the output PDF file (defaults to CSV filename with .pdf extension)" | |
| ) | |
| # Parse arguments | |
| args = parser.parse_args() | |
| # If pdf_file is not provided, use csv_file name with .pdf extension | |
| pdf_file = args.pdf_file if args.pdf_file else os.path.splitext(args.csv_file)[0] + ".pdf" | |
| csv_file = args.csv_file | |
| # Check if CSV file exists | |
| if not os.path.exists(csv_file): | |
| print(f"Error: CSV file '{csv_file}' not found!") | |
| return | |
| try: | |
| csv_to_pdf(csv_file, pdf_file) | |
| print(f"PDF generated successfully at '{pdf_file}'") | |
| except Exception as e: | |
| print(f"Error generating PDF: {e}") | |
| if __name__ == "__main__": | |
| main() |
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
| # Makefile for installing csv2pdf script | |
| SCRIPT_NAME = csv2pdf | |
| SCRIPT_SRC = csv2pdf.py | |
| INSTALL_DIR = /usr/local/bin | |
| PIP = pip3 | |
| PYTHON = python3 | |
| # Default target | |
| all: install | |
| # Install dependencies system-wide | |
| install-deps: | |
| $(PIP) install -r requirements.txt | |
| # Set executable permissions | |
| permissions: | |
| chmod +x $(SCRIPT_SRC) | |
| # Install script to /usr/local/bin | |
| install-script: permissions | |
| sudo cp $(SCRIPT_SRC) $(INSTALL_DIR)/$(SCRIPT_NAME) | |
| # Full installation (system-wide) | |
| install: install-deps install-script | |
| @echo "Installation complete. Run '$(SCRIPT_NAME) --help' to verify." | |
| # Clean up (placeholder, no virtual environment to clean) | |
| clean: | |
| @echo "No virtual environment to clean." | |
| # Remove script from /usr/local/bin | |
| uninstall: | |
| sudo rm -f $(INSTALL_DIR)/$(SCRIPT_NAME) | |
| @echo "Uninstalled $(SCRIPT_NAME) from $(INSTALL_DIR)." | |
| .PHONY: all install-deps permissions install-script install clean uninstall |
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
| pandas=2.0.0 | |
| matplotlib=3.5.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment