背景情報PythonのパンダXlsxWriterセンター選定アクロス
私がマージされ、ヘッダーを作成するため正常に動作するスクリプトを持っていますが、私はマージするのではなく、選択の間でセンタリングする必要があります。私は公式のドキュメンテーションに従ったが、うまくいかない。私は可能な組み合わせをたくさん試しましたが、そのうちのいくつかは写真に以下に含まれています。どんな助けでも大歓迎です。
コード
私はあなただけで、それを貼り付けxlsFilepath
を除いて、それを実行することができるようなコードを書きました。今のところ、単純にするために選択範囲を中心にテキストをハードコードしましたが、理想的にはこれをTitle
変数に変更します。
import pandas as pd
import numpy as np
#Create a random dataframe for this example
All_Columns_DF = pd.DataFrame(np.random.randn(100, 3), columns=['Title', 'Col_A' ,'Col_B'])
#Set file path for local machine
xlsFilepath = r'H:\myfile.xlsx'
#Create writer
writer = pd.ExcelWriter(xlsFilepath, engine='xlsxwriter')
#Write the DF to excel
All_Columns_DF.to_excel(writer, startrow = 1, sheet_name='Sheet1', index=False)
#Extract the header from the DF which I want to Center Across Selection in Row 1
Title_DF = All_Columns_DF[["Title"]]
Title_DF_Unique = Title_DF.drop_duplicates()
Title = Title_DF_Unique.iloc[0]['Title']
#Define WB and WS
workbook = writer.book
worksheet = writer.sheets['Sheet1']
#Start Formatting Attempt
format = workbook.add_format()
format.set_center_across()
''' Hard coding using row,col syntax does not work
worksheet.write(1, 1, 'Center across selection', format)
worksheet.write_blank(1, 2, '', format)
'''
'''Every combination of the below i try does not
center across the columns. Ive put write_blank above
below, started at col A, B, etc, not thing ive tried
works
'''
worksheet.write_blank("B1:F1", '', format)
worksheet.write("A1", 'Center across selection', format)
writer.save()
試み
最後に、私は、このパッケージに固執したいので、別のパッケージをお勧めしないでください。
恐ろしく、ありがとうございましたjmc! –
hello jmc、ちょうどあなたのプロフィールを見て、あなたが実際にxlsxwriterパッケージを作成したことに気付きました!それは素晴らしいです。私はちょうど興味があります、あなたはあなた自身のpythonパッケージを作る方法についてどうやって行くのですか?あなたはいくつかの承認プロセスを通じてPythonの許可を得なければなりませんか?このようなことをしてくれと指摘できる文書はありますか? –
[プロジェクトのパッケージングと配布](https://packaging.python.org/en/latest/distributing/)を参照してください。それはすべてのステップを説明します。 – jmcnamara