2

I have dataframes as output and I need to export to excel file. I can use pandas for the task but I need the output to be the worksheet from right to left direction. I have searched and didn't find any clue regarding using the pandas to change the direction .. I have found the package xlsxwriter do that

import xlsxwriter

workbook = xlsxwriter.Workbook('output.xlsx')
worksheet1 = workbook.add_worksheet()
format_right_to_left = workbook.add_format({'reading_order': 2})
worksheet1.set_column('A:A', 20)
worksheet1.right_to_left()
worksheet1.write(new_df)
workbook.close()

But I don't know how to export the dataframe using this approach ..

snapshot to clarify the directions: enter image description here

enter image description here

** I have used multiple lines as for format point

myformat = workbook.add_format()
myformat.set_reading_order(2)
myformat.set_align('center')
myformat.set_align('vcenter')

Is it possible to make such lines shorter using dictionary ..for example?

3
  • What do you mean by worksheet from right to left direction? Your expected output is not particularly clear to me. Commented Nov 30, 2020 at 21:01
  • If you know excel and VBA there is a property DisplayRightToLeft and it is assigned to True or False. I have found it in xlsxwriter package worksheet1.right_to_left() ..It is responsible for displaying the worksheet from left to rigth (this is the default) but as for Arabic, we are accustomed the direction from right to left. Commented Nov 30, 2020 at 21:06
  • Posted snapshots for more clarification. Commented Nov 30, 2020 at 21:09

1 Answer 1

3

You can do this:

import xlsxwriter

writer = pd.ExcelWriter('pandas_excel.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1') # Assuming you already have a `df`

workbook  = writer.book
worksheet = writer.sheets['Sheet1']

format_right_to_left = workbook.add_format({'reading_order': 2})
worksheet.right_to_left()
writer.save()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. As for such a line in VBA ws.Cells.ReadingOrder = xlRTL makes the Arabic numbers appear correctly. What is the equivalent here?
I have searched and could do that worksheet.set_column('A:G', 11, format_right_to_left). How can I add more formats? such as this cell_format.set_align('center'). How can I merge the formats ??
I have updated my question so as to make it clear as for the format point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.