8
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python(Pandas/XlsxWriter)を使ってExcelファイルを操作してみる①

Last updated at Posted at 2016-05-20

PandasというかXlsxWriterを使ってExcelファイルを操作してみる。
ちょっと触ってみた感じXlwingsの方が使いやすそうな感じもしたけど、
Pandasが使っているので、こっちを使ってみよう。

ファイルの読み込みについてはこちら

参照

pandas
XlsxWriter

サンプル

今回はExcelファイルを作成する編。

# import lib
import pandas as pd
import numpy as np

# create data
dates = pd.date_range("20130101", freq='D', periods=6)
df = pd.DataFrame(np.random.randn(6,4),index = dates, columns = list("ABCD"))

# set data
fileName = "hoge.xlsx"
writer = pd.ExcelWriter(fileName)
df.to_excel(writer, sheet_name="hoge1")
df.T.to_excel(writer, sheet_name="hoge2")

workbook  = writer.book
worksheet1 = writer.sheets['hoge1']
worksheet2 = writer.sheets['hoge2']

# Add some cell formats.
format1 = workbook.add_format()
format1.set_font_color('red')

index = workbook.add_format({'align': 'left'})
index.set_font_color('blue')

# Set the column width and format.
worksheet1.set_column('C:C', None, index)
worksheet2.set_column('B:B', 20, format1)

# save
writer.save()
8
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?