Pythonでxlsのデータを読み込む方法
6186 ワード
EXCELのデータを読み込むには、まずxlrdパッケージ、アドレスをダウンロードしなければなりません.https://pypi..org/pypi/xlrdインストール方法:解凍をダウンロードした後、windows dosコマンドで解凍ディレクトリegに入り、cd E:selenium--Pyton学習Pythonソフトウェアxlwt-1.0.0xlwt-1.0.0、次にコマンド:python setupを入力.py installでいいです.pythonでEXCELデータを読み込むには、次のコードを見てください.
EXCELにデータを書き込みます.
xlwtアドレス:http://pypi.python.org/pypi/xlwt
Excelファイルを作成し、Sheetを作成します.
1
2
3
4
Workbookクラスにはencodingとstyleがあります.compressionパラメータ.
encoding,文字符号化の設定,style_compressionは、圧縮するかどうかを表します.w=Workbook(encoding='utf-8')を設定すると、excelで中国語を出力できます.デフォルトはasciiです.
sheetへの書き込み:
1
簡単な書き込み:
1
フォーマットの書き込み:
1
2
3
4
5
6
7
8
9
書き込み日:
1
2
3
数式を書き込みます.
1
2
3
4
書き込みリンク:
1
転載先:https://www.cnblogs.com/HCT118/p/4501719.html
#coding=utf-8
import xlrd
my_file='E:\\baidu.xlsx'
book=xlrd.open_workbook(my_file)
print book.nsheets
print book.sheets()
print book.sheet_names()
#sheet = book.sheet_by_index(0) #
sheet = book.sheet_by_name(u'Sheet1')#
print sheet.nrows# xls
print sheet.ncols# xls
print sheet.name# sheet
print sheet.row(1)#
print sheet.row_values(1)#
print sheet.col(1)#
print sheet.col_values(1)#
cell=sheet.cell(2,1)# 2 1
cell_value = sheet.cell_value(2,1)# 2 1
#cell_value = sheet.cell(2,1).value# 2 1
print cell_value
EXCELにデータを書き込みます.
xlwtアドレス:http://pypi.python.org/pypi/xlwt
Excelファイルを作成し、Sheetを作成します.
1
2
3
4
from
xlwt
import
*
book
=
Workbook()
sheet
=
book.add_sheet(
'Sheet1'
)
book.save(
'myExcel.xls'
)
Workbookクラスにはencodingとstyleがあります.compressionパラメータ.
encoding,文字符号化の設定,style_compressionは、圧縮するかどうかを表します.w=Workbook(encoding='utf-8')を設定すると、excelで中国語を出力できます.デフォルトはasciiです.
sheetへの書き込み:
1
sheet.write(r, c, label
=
"", style
=
Style.default_style)
簡単な書き込み:
1
sheet.write(
0
,
0
, label
=
'Row 0, Column 0 Value'
)
フォーマットの書き込み:
1
2
3
4
5
6
7
8
9
font
=
xlwt.Font()
#
font.name
=
'Times New Roman'
font.bold
=
True
font.underline
=
True
font.italic
=
True
style
=
xlwt.XFStyle()
#
style.font
=
font
#
sheet.write(
1
,
0
, label
=
'Formatted value'
, style)
# Apply the Style to the Cell
book.save(
'myExcel.xls'
)
書き込み日:
1
2
3
style
=
xlwt.XFStyle()
style.num_format_str
=
'M/D/YY'
# Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0
sheet.write(
0
,
0
, datetime.datetime.now(), style)
数式を書き込みます.
1
2
3
4
sheet.write(
0
,
0
,
5
)
# Outputs 5
sheet.write(
0
,
1
,
2
)
# Outputs 2
sheet.write(
1
,
0
, xlwt.Formula(
'A1*B1'
))
# "10" (A1[5] * A2[2])
sheet.write(
1
,
1
, xlwt.Formula(
'SUM(A1,B1)'
))
# "7" (A1[5] + A2[2])
書き込みリンク:
1
sheet.write(
0
,
0
, xlwt.Formula(
'HYPERLINK("http://www.google.com";"Google")'
))
# "Google" http://www.google.com
転載先:https://www.cnblogs.com/HCT118/p/4501719.html