pythonxlwt下載
1. 如何安裝python xlwt
1、首先到下載xlwt-0.7.4.tar.gz;
2、把下載的.gz包解壓,如把它解壓到F:DOWNLOAD下。
3、下打開cmd,在命令行輸入F:,切換到F盤,再cd DOWNLOAD\xlrm-0.7.7,
4、最後 setup.py install.裝完了。
應用:
xlwt
import xlwt
filename = xlwt.Workbook ()
sheet = filename.add_sheet('name')
sheet.write(0,0,'hao123')
filename.save('test.xls')
2. Excel用Python讀取清洗後怎麼寫入數據
導入xlrd庫。
要導入xlrd庫,它是讀取excel中數據的庫,解壓所下載的壓縮包,用cmd命令行CD到解壓目錄,執行pythonsetup.pyinstall命令,要導入xlwt庫,它是開發人員用於生成與MicrosoftExcel版本95到2003兼容的電子表格文件的庫。接著用cmd命令行切換到下載文件所解壓的目錄,輸入pythonsetup.pyinstall命令,如無意外則安裝成功。
openpyxl是用於讀取和寫入Excel2010xlsx/xlsm/xltx/xltm文件的Python庫。
3. python怎麼讀寫excel文件
最近用python處理一個小項目,其中涉及到對excel的讀寫操作,通過查資料及實踐做了一下總結,以便以後用。
python讀寫excel文件要用到兩個庫:xlrd和xlwt,首先下載安裝這兩個庫。
1、#讀取Excel
importxlrd
data = xlrd.open_workbook(excelFile)
table = data.sheets()[0]
nrows = table.nrows #行數
ncols = table.ncols #列數
for i in xrange(0,nrows):
rowValues= table.row_values(i) #某一行數據
for item in rowValues:
printitem
2、寫Excel文件
'''往EXCEl單元格寫內容,每次寫一行sheet:頁簽名稱;row:行內容列表;rowIndex:行索引;
isBold:true:粗欄位,false:普通字體'''
defWriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
#style = xlwt.easyxf('font: bold 0, color red;')#紅色字體
#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 設置Excel單元格的背景色為黃色,字體為粗體
forsvalue inrowValueList:
strValue = unicode(str(svalue),'utf-8')
ifisBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i + 1
'''寫excel文件'''
defsave_Excel(strFile):
excelFile = unicode(strFile,"utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)
headList = ['標題1','標題2','標題3','標題4','總計']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,True)
fori inxrange(1,11):
rowIndex = rowIndex + 1
valueList = []
forj inxrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)
style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')
在設置上Excel單元格的背景色時,fore_colour支持的顏色是有限的,僅支持一下顏色
aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D"""
另外一種方式是 用pyExcelerator
4. python卸載後怎麼也安裝不上
本文主要給大家介紹了關於python卸載再安裝遇到問題的解決方法,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
卸載Python:
先在cmd下查看你的python版本,在網上下載相適應的安裝包卸載
如果uninstall卸載失敗,先Repair修復再卸載就可以了。
再安裝Python遇到問題無法安裝解決方法:
安裝時遇到verify that you have access to that directory . 用管理員身份運行即可。
pip遇到的問題
如pip install xlwt失敗出現You are using pip version 10.0.1, however version 19.1.1 is available.
解決:python -m pip install --upgrade pip
如:如pip install xlwt失敗出現Consider using the --user option or check the permissions.
解決:pip install --user xlwt
更多Python相關技術文章,請訪問Python教程欄目進行學習!以上就是小編分享的關於python卸載後怎麼也安裝不上的詳細內容希望對大家有所幫助,更多有關python教程請關注環球青藤其它相關文章!
5. 怎麼用python讀取excel表格的數據
一、讀excel表
讀excel要用到xlrd模塊,官網安裝(http://pypi.python.org/pypi/xlrd)。然後就可以跟著裡面的例子稍微試一下就知道怎麼用了。大概的流程是這樣的:
1、導入模塊
復制代碼代碼如下:
import xlrd
2、打開Excel文件讀取數據
復制代碼代碼如下:
data = xlrd.open_workbook('excel.xls')
3、獲取一個工作表
① table = data.sheets()[0] #通過索引順序獲取
② table = data.sheet_by_index(0) #通過索引順序獲取
③ table = data.sheet_by_name(u'Sheet1')#通過名稱獲取
4、獲取整行和整列的值(返回數組)
復制代碼代碼如下:
table.row_values(i)
table.col_values(i)
5、獲取行數和列數
復制代碼代碼如下:
table.nrows
table.ncols
6、獲取單元格
復制代碼代碼如下:
table.cell(0,0).value
table.cell(2,3).value
就我自己使用的時候覺得還是獲取cell最有用,這就相當於是給了你一個二維數組,餘下你就可以想怎麼干就怎麼幹了。得益於這個十分好用的庫代碼很是簡潔。但是還是有若干坑的存在導致話了一定時間探索。現在列出來供後人參考吧:
1、首先就是我的統計是根據姓名統計各個表中的信息的,但是調試發現不同的表中各個名字貌似不能夠匹配,開始懷疑過編碼問題,不過後來發現是因為空格。因為在excel中輸入的時候很可能會順手在一些名字後面加上幾個空格或是tab鍵,這樣看起來沒什麼差別,但是程序處理的時候這就是兩個完全不同的串了。我的解決方法是給每個獲取的字元串都加上strip()處理一下。效果良好
2、還是字元串的匹配,在判斷某個單元格中的字元串(中文)是否等於我所給出的的時候發現無法匹配,並且各種unicode也不太奏效,網路過一些解決方案,但是都比較復雜或是沒用。最後我採用了一個比較變通的方式:直接從excel中獲取我想要的值再進行比較,效果是不錯就是通用行不太好,個呢不能問題還沒解決。
二、寫excel表
寫excel表要用到xlwt模塊,官網下載(http://pypi.python.org/pypi/xlwt)。大致使用流程如下:
1、導入模塊
復制代碼代碼如下:
import xlwt
2、創建workbook(其實就是excel,後來保存一下就行)
復制代碼代碼如下:
workbook = xlwt.Workbook(encoding = 'ascii')
3、創建表
復制代碼代碼如下:
worksheet = workbook.add_sheet('My Worksheet')
4、往單元格內寫入內容
復制代碼代碼如下:
worksheet.write(0, 0, label = 'Row 0, Column 0 Value')
5、保存
復制代碼代碼如下:
workbook.save('Excel_Workbook.xls')
由於我的需求比較簡單,所以這上面沒遇到什麼問題,唯一的就是建議還是用ascii編碼,不然可能會有一些詭異的現象。
當然xlwt功能遠遠不止這些,他甚至可以設置各種樣式之類的。附上一點例子
復制代碼代碼如下:
Examples Generating Excel Documents Using Python's xlwt
Here are some simple examples using Python's xlwt library to dynamically generate Excel documents.
Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files for LibreOffice / OpenOffice. You can check them out at:http://packages.python.org/ezodf/index.html
The Simplest Example
import xlwt
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet')
worksheet.write(0, 0, label = 'Row 0, Column 0 Value')
workbook.save('Excel_Workbook.xls')
Formatting the Contents of a Cell
import xlwt
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet')
font = xlwt.Font() # Create the Font
font.name = 'Times New Roman'
font.bold = True
font.underline = True
font.italic = True
style = xlwt.XFStyle() # Create the Style
style.font = font # Apply the Font to the Style
worksheet.write(0, 0, label = 'Unformatted value')
worksheet.write(1, 0, label = 'Formatted value', style) # Apply the Style to the Cell
workbook.save('Excel_Workbook.xls')
Attributes of the Font Object
font.bold = True # May be: True, False
font.italic = True # May be: True, False
font.struck_out = True # May be: True, False
font.underline = xlwt.Font.UNDERLINE_SINGLE # May be: UNDERLINE_NONE, UNDERLINE_SINGLE, UNDERLINE_SINGLE_ACC, UNDERLINE_DOUBLE, UNDERLINE_DOUBLE_ACC
font.escapement = xlwt.Font.ESCAPEMENT_SUPERSCRIPT # May be: ESCAPEMENT_NONE, ESCAPEMENT_SUPERSCRIPT, ESCAPEMENT_SUBSCRIPT
font.family = xlwt.Font.FAMILY_ROMAN # May be: FAMILY_NONE, FAMILY_ROMAN, FAMILY_SWISS, FAMILY_MODERN, FAMILY_SCRIPT, FAMILY_DECORATIVE
font.charset = xlwt.Font.CHARSET_ANSI_LATIN # May be: CHARSET_ANSI_LATIN, CHARSET_SYS_DEFAULT, CHARSET_SYMBOL, CHARSET_APPLE_ROMAN, CHARSET_ANSI_JAP_SHIFT_JIS, CHARSET_ANSI_KOR_HANGUL, CHARSET_ANSI_KOR_JOHAB, CHARSET_ANSI_CHINESE_GBK, CHARSET_ANSI_CHINESE_BIG5, CHARSET_ANSI_GREEK, CHARSET_ANSI_TURKISH, CHARSET_ANSI_VIETNAMESE, CHARSET_ANSI_HEBREW, CHARSET_ANSI_ARABIC, CHARSET_ANSI_BALTIC, CHARSET_ANSI_CYRILLIC, CHARSET_ANSI_THAI, CHARSET_ANSI_LATIN_II, CHARSET_OEM_LATIN_I
font.colour_index = ?
font.get_biff_record = ?
font.height = 0x00C8 # C8 in Hex (in decimal) = 10 points in height.
font.name = ?
font.outline = ?
font.shadow = ?
Setting the Width of a Cell
import xltw
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write(0, 0, 'My Cell Contents')
worksheet.col(0).width = 3333 # 3333 = 1" (one inch).
workbook.save('Excel_Workbook.xls')
Entering a Date into a Cell
import xlwt
import datetime
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
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
worksheet.write(0, 0, datetime.datetime.now(), style)
workbook.save('Excel_Workbook.xls')
Adding a Formula to a Cell
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write(0, 0, 5) # Outputs 5
worksheet.write(0, 1, 2) # Outputs 2
worksheet.write(1, 0, xlwt.Formula('A1*B1')) # Should output "10" (A1[5] * A2[2])
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # Should output "7" (A1[5] + A2[2])
workbook.save('Excel_Workbook.xls')
Adding a Hyperlink to a Cell
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) # Outputs the text "Google" linking to http://www.google.com
workbook.save('Excel_Workbook.xls')
Merging Columns and Rows
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.
font = xlwt.Font() # Create Font
font.bold = True # Set font to Bold
style = xlwt.XFStyle() # Create Style
style.font = font # Add Bold Font to Style
worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.
workbook.save('Excel_Workbook.xls')
Setting the Alignment for the Contents of a Cell
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
alignment = xlwt.Alignment() # Create Alignment
alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
style = xlwt.XFStyle() # Create Style
style.alignment = alignment # Add Alignment to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('Excel_Workbook.xls')
Adding Borders to a Cell
# Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
borders = xlwt.Borders() # Create Borders
borders.left = xlwt.Borders.DASHED # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style = xlwt.XFStyle() # Create Style
style.borders = borders # Add Borders to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('Excel_Workbook.xls')
Setting the Background Color of a Cell
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
pattern = xlwt.Pattern() # Create the Pattern
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
style = xlwt.XFStyle() # Create the Pattern
style.pattern = pattern # Add Pattern to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('Excel_Workbook.xls')
TODO: Things Left to Document
- Panes -- separate views which are always in view
- Border Colors (documented above, but not taking effect as it should)
- Border Widths (document above, but not working as expected)
- Protection
- Row Styles
- Zoom / Manification
- WS Props?
Source Code for reference available at: https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/
6. 怎麼用python讀取txt文件里指定行的內容,並導入excel
def eachlineof(filename):
''' 逐行讀取給定的文本文件,返回行號、剔除末尾空字元的行內容 '''
with open(filename) as handle:
for lno, line in enumerate(handle):
yield lno+1, line.strip()
另外: 讀寫excel需要第三方類庫,可以考慮下載安裝xlrd, xlwt
寫excel表
寫excel表要用到xlwt模塊,官網下載(http://pypi.python.org/pypi/xlwt)。大致使用流程如下:
1、導入模塊
復制代碼代碼如下:
import xlwt
2、創建workbook(其實就是excel,後來保存一下就行)
復制代碼代碼如下:
workbook = xlwt.Workbook(encoding = 'ascii')
3、創建表
復制代碼代碼如下:
worksheet = workbook.add_sheet('My Worksheet')
4、往單元格內寫入內容
復制代碼代碼如下:
worksheet.write(0, 0, label = 'Row 0, Column 0 Value')
5、保存
復制代碼代碼如下:
workbook.save('Excel_Workbook.xls')
由於我的需求比較簡單,所以這上面沒遇到什麼問題,唯一的就是建議還是用ascii編碼,不然可能會有一些詭異的現象。
當然xlwt功能遠遠不止這些,他甚至可以設置各種樣式之類的。附上一點例子
復制代碼代碼如下:
Examples Generating Excel Documents Using Python's xlwt
Here are some simple examples using Python's xlwt library to dynamically generate Excel documents.
Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files for LibreOffi
7. 如何將一個python中的參數寫入excel中
import xlwt
f = xlwt.Workbook() #創建工作薄
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #創建sheet
list = [1,2,3,4,5]
j = 0
for i in list:
sheet1.write(j,0,i) #循環版寫入 豎著寫
j=j+1
f.save('text.xls')#保存權文件