Replies: 4 comments 1 reply
-
Thank you for your issue. Give us a little time to review it. PS. You might want to check the FAQ if you haven't done so already. This is an automated reply, generated by FAQtory |
Beta Was this translation helpful? Give feedback.
-
Exporting a PNG would require an additional dependencies that I wouldn't want to add I'm afraid. |
Beta Was this translation helpful? Give feedback.
-
I understand & appreciate your work, but is this possible to add an option to the Source Code# pip install pandas
import pandas as pd
# pip install rich
from rich.table import Table
from rich.console import Console
from rich.terminal_theme import MONOKAI
# To name files temporarily
import time
# sudo apt-get install inkscape (~360 MB)
import subprocess as sp
# pip install PyMuPDF
# PyMuPDF - Convert PDF to PNG
import fitz
# BytesIO - Save images in memory
from io import BytesIO
# pahtlib.Path - Delete unnecessary files
from pathlib import Path
def rich_svg_pdf_png():
# Stores BytesIO PNGs
ListOfPNGsBytesIO = list()
# Create a DataFrame
df = pd.DataFrame([('User1', '🐧', 180, 40),
('User2', '└', 200, 50),
('User3', '𓁳', 210, 30)],
columns=('Name', 'Character', 'Height', 'Score'))
# Select the fields to display in the table
table_data = df[
[
"Name", "Character", "Height",
"Score",
]
]
# Create a table
#table = Table(title="Data Table", title_justify="center")
table = Table()
table.add_column("N", justify="center")
table.add_column("Name", justify="center")
table.add_column("Character", justify="center")
table.add_column("Height", justify="center")
table.add_column("Score", justify="center")
# Add rows to the table
for index, row in enumerate(table_data.itertuples()):
table.add_row(
str(index), str(row.Name), str(row.Character),
str(row.Height), str(row.Score)
)
# Set the width of the table
table.width = 115
# Create a console
console = Console(record=True)
# Print the table
console.print(table)
# Set a name for SVG file, then delete it using pathlib.Path.unlink
# tempnowtime, remove . from floating time to have a integrated integer number
tempnowtime = str(time.time()).replace(".", "")
SVGFileName = f"{tempnowtime}.svg"
# Save as SVG file
console.save_svg(
path=SVGFileName, title=f"Users Table",
theme=MONOKAI
)
PDFFileName = f"{tempnowtime}.pdf"
# Convert SVG to PDF
sp.run(["inkscape", SVGFileName, "-o", PDFFileName], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
# Open the PDF file
pdf_doc = fitz.open(PDFFileName)
# Iterate through the pages of the PDF file
for page_num in range(pdf_doc.page_count):
# Select the page
pdf_page = pdf_doc.load_page(page_num)
""" # Do something with the page, for example, save it as an image
pix = pdf_page.get_pixmap()
pix.save(f'page_{page_num + 1}.png') """
# Render the page as a pixmap with a resolution of 300 DPI
pixmap = pdf_page.get_pixmap(dpi=300)
# Convert the pixmap to PNG bytes
png_bytes = BytesIO(pixmap.tobytes("png"))
# Append the png_bytes to the list
ListOfPNGsBytesIO.append(png_bytes)
# Close the PDF file
pdf_doc.close()
# Delete the unnecessary files
resolve_svg = Path(SVGFileName).resolve()
resolve_pdf = Path(PDFFileName).resolve()
Path.unlink(resolve_svg)
Path.unlink(resolve_pdf)
return ListOfPNGsBytesIO
# Get BytesIO pics
get_pics = rich_svg_pdf_png()
# Iterate thorugh pics and write them as PNG file
for number, pic in enumerate(get_pics):
with open(f"pic{number}.png", "wb") as f:
f.write(pic.getbuffer()) |
Beta Was this translation helpful? Give feedback.
-
https://github.com/FHPythonUtils/AnsiToImg might fit your use case? This stands on the shoulders of Textualize/rich (originally, I used custom implementations for these kind of exports). Hope this helps :) |
Beta Was this translation helpful? Give feedback.
-
Hi, first of all, I want to say thanks for your awesome work.
It's been about 4 hours that I'm trying to get the beautiful syntax-highlighted SVG as PNG using libraries such as
wand
,svglib
&cairosvg
... But they do not generate a high-quality PNG, and many times they're struggling with the font and sometimes things like alpha channels.If you add such an ability to get PNG out of our console (As BytesIO so we decide to send it and delete it from the memory or as .png file), that would be great. It would benefit many developers.
Thanks and Keep up the great work.
Beta Was this translation helpful? Give feedback.
All reactions