Why is my color blue gone in Python fpdf? /u/maiasub Python Education

https://drive.google.com/drive/folders/1BGdi8AIeiaIcx8rqouQwmdxZdqd5s-Us?usp=sharing

I wanna:

For each row of the gerthemedeck1.xlsx, if the first column is “der”, make the second column’s text blue.

And if the first column is “die”, make the second column’s text red.

And if the first column is “das”, make the second column’s text green.

import pandas as pd from fpdf import FPDF class PDF(FPDF): def __init__(self): super().__init__() self.add_page() # Add a Unicode-compatible font self.add_font('Courier New', '', r'C:WindowsFontscour.ttf', uni=True) # Update with the correct path self.set_font("Courier New", size=14) # Set font size to 14 def add_row(self, first_four, fifth_col, colors): """Add a row to the PDF with alignment and color.""" # Adjust widths based on the value of the first column if first_four[0].lower().strip() in colors: # Check if the first column is "der", "die", or "das" widths = [15, 120, 80, 40] # Reduced width for the first column else: widths = [80, 120, 80, 40] # Default widths for i, cell in enumerate(first_four): # Handle color adjustment for second column based on the first column if i == 1 and first_four[0].lower().strip() in colors: # Apply color to the second column based on the first column self.set_text_color(*colors[first_four[0].lower().strip()]) else: self.set_text_color(0, 0, 0) # Default to black # Write the cell with adjusted width (no wrapping in first four columns) self.cell(widths[i], 10, txt=cell, border=1) self.ln() # Move to the next line # Reset color to black for the fifth column self.set_text_color(0, 0, 0) if fifth_col: # Use multi_cell for the fifth column to wrap the text if it's too long self.multi_cell(0, 10, txt=fifth_col, border=1, align='L') self.ln(5) # Add spacing between rows # Path to the Excel file file_path = 'gerthemedeck1.xlsx' # Read the Excel file df = pd.read_excel(file_path) # Create an instance of the PDF class pdf = PDF() # Define colors for "der", "die", and "das" colors = { "der": (0, 0, 255), # Blue "die": (255, 0, 0), # Red "das": (0, 255, 0), # Green } # Process rows for _, row in df.iterrows(): # Extract the first four non-empty columns first_four = [str(cell) for cell in row[:4] if pd.notna(cell)] fifth_col = str(row[4]) if pd.notna(row[4]) else "" # Add the aligned row to the PDF pdf.add_row(first_four, fifth_col, colors) # Save the PDF output_pdf_path = 'output_corrected.pdf' pdf.output(output_pdf_path) print(f"PDF exported to {output_pdf_path}") 

submitted by /u/maiasub
[link] [comments]

​r/learnpython https://drive.google.com/drive/folders/1BGdi8AIeiaIcx8rqouQwmdxZdqd5s-Us?usp=sharing I wanna: For each row of the gerthemedeck1.xlsx, if the first column is “der”, make the second column’s text blue. And if the first column is “die”, make the second column’s text red. And if the first column is “das”, make the second column’s text green. import pandas as pd from fpdf import FPDF class PDF(FPDF): def __init__(self): super().__init__() self.add_page() # Add a Unicode-compatible font self.add_font(‘Courier New’, ”, r’C:WindowsFontscour.ttf’, uni=True) # Update with the correct path self.set_font(“Courier New”, size=14) # Set font size to 14 def add_row(self, first_four, fifth_col, colors): “””Add a row to the PDF with alignment and color.””” # Adjust widths based on the value of the first column if first_four[0].lower().strip() in colors: # Check if the first column is “der”, “die”, or “das” widths = [15, 120, 80, 40] # Reduced width for the first column else: widths = [80, 120, 80, 40] # Default widths for i, cell in enumerate(first_four): # Handle color adjustment for second column based on the first column if i == 1 and first_four[0].lower().strip() in colors: # Apply color to the second column based on the first column self.set_text_color(*colors[first_four[0].lower().strip()]) else: self.set_text_color(0, 0, 0) # Default to black # Write the cell with adjusted width (no wrapping in first four columns) self.cell(widths[i], 10, txt=cell, border=1) self.ln() # Move to the next line # Reset color to black for the fifth column self.set_text_color(0, 0, 0) if fifth_col: # Use multi_cell for the fifth column to wrap the text if it’s too long self.multi_cell(0, 10, txt=fifth_col, border=1, align=’L’) self.ln(5) # Add spacing between rows # Path to the Excel file file_path = ‘gerthemedeck1.xlsx’ # Read the Excel file df = pd.read_excel(file_path) # Create an instance of the PDF class pdf = PDF() # Define colors for “der”, “die”, and “das” colors = { “der”: (0, 0, 255), # Blue “die”: (255, 0, 0), # Red “das”: (0, 255, 0), # Green } # Process rows for _, row in df.iterrows(): # Extract the first four non-empty columns first_four = [str(cell) for cell in row[:4] if pd.notna(cell)] fifth_col = str(row[4]) if pd.notna(row[4]) else “” # Add the aligned row to the PDF pdf.add_row(first_four, fifth_col, colors) # Save the PDF output_pdf_path = ‘output_corrected.pdf’ pdf.output(output_pdf_path) print(f”PDF exported to {output_pdf_path}”) submitted by /u/maiasub [link] [comments] 

https://drive.google.com/drive/folders/1BGdi8AIeiaIcx8rqouQwmdxZdqd5s-Us?usp=sharing

I wanna:

For each row of the gerthemedeck1.xlsx, if the first column is “der”, make the second column’s text blue.

And if the first column is “die”, make the second column’s text red.

And if the first column is “das”, make the second column’s text green.

import pandas as pd from fpdf import FPDF class PDF(FPDF): def __init__(self): super().__init__() self.add_page() # Add a Unicode-compatible font self.add_font('Courier New', '', r'C:WindowsFontscour.ttf', uni=True) # Update with the correct path self.set_font("Courier New", size=14) # Set font size to 14 def add_row(self, first_four, fifth_col, colors): """Add a row to the PDF with alignment and color.""" # Adjust widths based on the value of the first column if first_four[0].lower().strip() in colors: # Check if the first column is "der", "die", or "das" widths = [15, 120, 80, 40] # Reduced width for the first column else: widths = [80, 120, 80, 40] # Default widths for i, cell in enumerate(first_four): # Handle color adjustment for second column based on the first column if i == 1 and first_four[0].lower().strip() in colors: # Apply color to the second column based on the first column self.set_text_color(*colors[first_four[0].lower().strip()]) else: self.set_text_color(0, 0, 0) # Default to black # Write the cell with adjusted width (no wrapping in first four columns) self.cell(widths[i], 10, txt=cell, border=1) self.ln() # Move to the next line # Reset color to black for the fifth column self.set_text_color(0, 0, 0) if fifth_col: # Use multi_cell for the fifth column to wrap the text if it's too long self.multi_cell(0, 10, txt=fifth_col, border=1, align='L') self.ln(5) # Add spacing between rows # Path to the Excel file file_path = 'gerthemedeck1.xlsx' # Read the Excel file df = pd.read_excel(file_path) # Create an instance of the PDF class pdf = PDF() # Define colors for "der", "die", and "das" colors = { "der": (0, 0, 255), # Blue "die": (255, 0, 0), # Red "das": (0, 255, 0), # Green } # Process rows for _, row in df.iterrows(): # Extract the first four non-empty columns first_four = [str(cell) for cell in row[:4] if pd.notna(cell)] fifth_col = str(row[4]) if pd.notna(row[4]) else "" # Add the aligned row to the PDF pdf.add_row(first_four, fifth_col, colors) # Save the PDF output_pdf_path = 'output_corrected.pdf' pdf.output(output_pdf_path) print(f"PDF exported to {output_pdf_path}") 

submitted by /u/maiasub
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *