練習workbook = xlsxwriter.workbook(『practice.xlsx』)
worksheet = workbook.add_worksheet(『1』)
font=[『font_name』, 『font_size』, 『font_color』,『bold』, 『italic』, 『underline』, 『font_strikeout』, 『font_script』]
#color的選項是[black] [blue] [cyan] [green] [magenta] [red] [white] [yellow] 或者用#rrggbb 16色編碼
#fontname的選項是所有excel中裝的 『times new roman』, italic 斜體
#font_script 上下標 選項有 1 = superscript; 2 = subscript
number=[『num_format』]
protection=[『locked』, 『hidden』]
alignment=[『align』, 『valign』, 『rotation』, 『text_wrap』, 『reading_order』, 『text_justlast』, 『center_across』, 『indent』, 『shrink』]
#align 的選項有 left, center, right, fill, justify, center_across, distributed,
#valign 包含top, vcenter, bottom, vjustify, vdistributed
#texp_wrap 換行 indent 縮排 center across 中心跨列
pattern=[『pattern』, 『bg_color』, 『fg_color』]
#pattern:圖案樣式 bg_color:背景顏色 fg_color:foreground color
border=[『border』, 『bottom』, 『top』, 『left』, 『right』, 『border_color』, 『top_color』, 『left_color』,『right_color』]
#border 的選項有 13 個, 0是none; 7是weight是1的虛線;
date_format1 = workbook.add_format()
date_format2 = workbook.add_format()
date_format3 = workbook.add_format()
number_format1 = workbook.add_format()
number_format2 = workbook.add_format()
money_format = workbook.add_format() #千位分隔符
zip_format= workbook.add_format()
text_wrap_format = workbook.add_format()
merge_format = workbook.add_format()
第一種方法:
conditional_format = workbook.add_format()
worksheet.write(10, 0, 123, conditional_format) # > 0 green
worksheet.write(11, 0, -45, conditional_format) # < 0 red
worksheet.write(12, 0, 0, conditional_format) # = 0 default color
第二種方法:
worksheet.conditional_format(『b3:k12』, )
worksheet.write(『a1』, 1000, money_format)
worksheet.write(『a2』, 42254.415, date_format1)
worksheet.write(『a3』, 42254.415, date_format2)
worksheet.write(『a4』, 42254.415, date_format3)
worksheet.write(『b1』, 42254.415, number_format1)
worksheet.write(『b2』, 42254.415, number_format2)
worksheet.write(『c1』, 215, zip_format)
worksheet.write(『e1』, 「text_wrap_format」, text_wrap_format)
worksheet.write(『e2』, 「text_wrap_format\nmanually」, text_wrap_format)
寫日期date = datetime.datetime.strptime(『20181010』,』%y%m%d』)
worksheet.write_datetime(『d1』, date, date_format1)
寫文字worksheet.write_string(『d2』, 『string』, number_format1)
worksheet.write_number (『d3』, 100000000, number_format1)
寫函式worksheet.write_formula(『d4』, 『=sum(b1:b4』)
worksheet.write_array_formula(『d5:d7』, 『』, number_format2)
worksheet.write_blank(『d8』, none, money_format)
在乙個單元格中的文字使用不同格式(富文字)
bold = workbook.add_format()
italic = workbook.add_format()
worksheet.write_rich_string(『d9』,
'this is ',
bold, 『bold』,
』 and this is ',
italic, 『italic』)
一下子寫一行/列
data = (『foo』, 『bar』, 『baz』)
worksheet.write_row(『d10』, data)
worksheet.write_column(『d11』, data)
worksheet.set_column(12, 4, 15) #12列寬度為15
worksheet.set_row(13, 18)
cell_format = workbook.add_format()
worksheet.set_row(14, none, cell_format, )
worksheet.set_row(0, none, none, )
for worksheet in workbook.worksheets():
print(worksheet.get_name())
worksheet.merge_range(15, 4, 17, 4, 『merged cells』, merge_format)
worksheet.freeze_panes(『a2』) # same using a1 notation.
worksheet.hide_zero()
worksheet.set_tab_color(『red』)
#worksheet2.set_tab_color(』#ff9900』) # orange
#worksheet.protect()
worksheet.unprotect_range(『a1』)
#worksheet.ignore_errors()
workbook.close()
通過python讀取excel寫入mysql
import pymysql import xlrd import sys from xlrd import xldate as datetime excel檔案的路徑 excel path sys.ar 1 host sys.ar 2 user sys.ar 3 password sys.ar 4...
python實現excel內容逐行寫入txt
最近在做文字分類,拿到的資料很亂。要做下一步,不管是分詞還是tfidf都要先做資料的分類。3萬篇文章,在乙個excel中,每行有每篇文章的id 內容 title content 分類 relative breeds 共三列 按分類建立子目錄,文章按分類放入子目錄中,每篇文章寫入乙個txt檔案,txt...
PYTHON如何讀取和寫入EXCEL裡面的資料
好久沒寫了,今天來說說python讀取excel的常見方法。首先需要用到xlrd模組,pip install xlrd 安裝模組。首先開啟excel檔案 xl xlrd.open workbook r d file data.xlsx 傳檔案路徑 通過索引獲取要操作的工作表 table xl.she...