使用python将阿拉伯语数据帧转换为pdf



基本上,我想要一个将Arabic dataFrame转换为pdf文件的代码当我使用这个代码时

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()

它导出它,但带有空单元格

我发现了一个代码,可以在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

最新更新