Arabic dataframe to pdf using python

Viewed 23

basically, I want a code that converts Arabic dataFrame to a pdf file when I use this code

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from pandas.plotting import table


fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=report.values,colLabels=report.columns,loc='center')

pp = PdfPages("foo.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()

it export it but with empty cells

1 Answers

I found a code that can make Arabic letters display in the pdf

#the libraries I used
from fpdf import FPDF
import arabic_reshaper
from bidi.algorithm import get_display
import subprocess

def arabic_text (text): #function to change مرحبا to ابحرم (will make fpdf read it)
    reshaped_text = arabic_reshaper.reshape(text)
    bidi_text = get_display(reshaped_text)
    return bidi_text

pdf = FPDF() #call FPDF
# I downloaded it from this URL https://arbfonts.com/noto-sans-arabic-regular-font-download.html
pdf.add_font("NotoSansArabic", style="", fname="NotoSansArabic-Regular.ttf",uni=True) #add the font
pdf.add_page() #add page
pdf.set_font('NotoSansArabic', '', 8) # set font
pdf.set_xy(5, 5) #set x and y
pdf.cell(0, 0, arabic_text("مرحبا"), align='c') #the word i want to type
try:
    pdf.output('hi.pdf', 'F') # get the pdf file
    subprocess.Popen(['hi.pdf'], shell=True) #open it
except:
    print("Close the pdf file") #message if the pdf is opened
Related