gpt4 book ai didi

python - pandas ExcelWriter 从右到左写入工作表

转载 作者:行者123 更新时间:2023-12-01 07:26:32 27 4
gpt4 key购买 nike

我正在 python Django 中创建下载的动态 Excel 文件(未存储在本地),我需要使工作表从右到左显示,而不是从左到右显示。

这是我使用的代码:


import pandas
from io import BytesIO, StringIO

sio = BytesIO()

PandasDataFrame = pandas.DataFrame([['t1', 't2'], ['t3', 't4']], index = ['t1', 't2'], columns = ['t11', 't1'] )

PandasWriter = pandas.ExcelWriter(sio, engine='xlsxwriter')



PandasDataFrame.to_excel(PandasWriter, sheet_name='Sheet1')

PandasWriter.book.add_format({'reading_order': 2})

PandasWriter.save()

PandasWriter.close()

sio.seek(0)

workbook = sio.read()

response = HttpResponse(workbook,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

response['Content-Disposition'] = 'attachment; filename=wrong_data.xlsx'

return (response)

最佳答案

这是一个简单的 Pandas 示例,演示了如何更改文本以及工作表方向。您可以自己将其转换为 Django 示例。


# _*_ coding: utf-8

import pandas as pd

# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [u'نص عربي / English text'] * 6})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']

# Add the cell formats.
format_right_to_left = workbook.add_format({'reading_order': 2})

# Change the direction for the worksheet.
worksheet.right_to_left()

# Make the column wider for visibility and add the reading order format.
worksheet.set_column('B:B', 30, format_right_to_left)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

输出:

enter image description here

请参阅 the worksheet right_to_left() 上的 XlsxWriter 文档方法了解更多详细信息。

关于python - pandas ExcelWriter 从右到左写入工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com