Python-XlsxWriterモジュールを使用してExcelシートにさまざまな種類のスタイルチャートをプロットする
XlsxWriterはPythonライブラリであり、Excelファイルに対して、作成、書き込み、算術演算、グラフのプロットなどの複数の演算を実行できます。
例
# import xlsxwriter module
import xlsxwriter
# Workbook() takes one, non-optional, argument which is the filename #that we want to create.
workbook = xlsxwriter.Workbook('chart_styles.xlsx')
#Show the styles for column and area chart types.
chart_types = ['column', 'area']
for chart_type in chart_types:
# The workbook object is then used to add new worksheet via the #add_worksheet() method.
# Add a worksheet for each chart type
worksheet = workbook.add_worksheet(chart_type.title())
# set zoom option
worksheet.set_zoom(30)
# initialize style
style_number = 1
# Create 48 built-in styles, each with a different style.
# each chart dimension is 15 X 8.
for row_num in range(0, 90, 15):
for col_num in range(0, 64, 8):
# Create a chart object that can be added
# to a worksheet using add_chart() method.
# here we create a respective chart object .
chart = workbook.add_chart({'type': chart_type})
# Add a data series to a chart
# using add_series method.
chart.add_series({'values': '= Data !$A$1:$A$6'})
# Add a chart title
chart.set_title ({'name': 'Style % d' % style_number})
# Turn off the chart legend.
chart.set_legend({'none': True})
# Set an Excel chart style.
chart.set_style(style_number)
# add chart to the worksheet
# at the top-left corner of a chart is anchored to
# respective position of cell.
worksheet.insert_chart(row_num, col_num, chart)
# do increment
style_number += 1
# The workbook object is then used to add new worksheet via the #add_worksheet() method.
# create a worksheet for writing data.
data_worksheet = workbook.add_worksheet('Data')
# create a data list .
data = [10, 40, 50, 20, 10, 50]
# Write a column of data starting from 'A1'
data_worksheet.write_column('A1', data)
# hide the data worksheet
data_worksheet.hide()
# Finally, close the Excel file via the close() method.
workbook.close() -
PythonXlsxWriterモジュールを使用してExcelシートにチャートシートを追加する
Python独自のライブラリに加えて、個々の作成者によって作成された多くの外部ライブラリがあり、Pythonで追加機能を作成するのに最適です。 Xlsxライブラリは、Pythonプログラムからのデータを含むExcelファイルを作成するだけでなく、グラフも作成するそのようなライブラリの1つです。 円グラフの作成 次の例では、xlsxwriterライターを使用して円グラフを作成します。ここでは、最初にワークブックを定義し、次のステップでそれにワークシートを追加します。データを定義し、円グラフを定義する列に基づいて、データがExcelファイルに保存される列を決定します。ワークシート内の特定の場所。
-
Pythonopenpyxlモジュールを使用してExcelファイルの読み取りと書き込み
Pythonは、Excelファイルを操作するためのopenpyxlモジュールを提供します。 このモジュールでは、Excelファイルの作成方法、書き込み方法、読み取り方法などを実装できます。 openpyxlモジュールをインストールするには、コマンドプロンプトでこのコマンドを記述できます pip install openpyxl シートにタイトル名を付けたい場合 サンプルコード import openpyxl my_wb = openpyxl.Workbook() my_sheet = my_wb.active my_sheet_title = my_sheet.title print