當前位置:首頁 » 編程語言 » python3xlwt

python3xlwt

發布時間: 2022-07-15 13:33:45

python用xlwt向xls中寫入中文,顯示亂碼該怎麼解決

亂碼是編碼問題,微軟的excel默認打開的編碼是gbk,如果你寫入的數據是非gbk編碼的,那麼使用微軟excel打開就會出現亂碼的情況。解決方法為:對要寫入的數據編碼為gbk之後在進行寫入,在python中的簡單的操作方法如下:

1.源數據為unicode編碼,則直接使用encode("gbk")即可轉換編碼;

str=u"我愛中國共產黨"#unicode編碼的字元串
des_str=str.encode("gbk")#轉換為gbk編碼

2.源數據不是unicode編碼,那麼必須先使用decode函數解碼為unicode編碼,在按照第一種情況轉換為gbk編碼。

str=u"我愛中國共產黨"#unicode編碼的字元串
src_str=str.encode("utf-8")#源數據格式utf-8
des_str=str.decode("utf-8").encode("gbk")#轉換為gbk編碼

注意:encode和decode方法為字元串的方法,不能應用與列表等其他類型的數據上

㈡ 想問下python3怎麼將列表數據逐行寫入excel表格呢

import xlwtf = xlwt.Workbook() #創建工作薄sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #創建sheetlist = [1,2,3,4,5]j = 0for i in list:sheet1.write(j,0,i) #循環寫入 豎

㈢ 如何安裝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')

㈣ python xlwt怎麼給單元格中部分字設置顏色

#coding=utf-8

import xlwt

#file = xlwt.Workbook(encoding = 'utf-8')

wbk = xlwt.Workbook(encoding = 'utf-8')

sheet = wbk.add_sheet('sheet 1', cell_overwrite_ok=True)

#這樣表單就被創建了,寫入數據也很簡單:

# indexing is zero based, row then column

#sheet2 = wbk.add_sheet('sheet 2', cell_overwrite_ok=True)

style = xlwt.XFStyle()

font = xlwt.Font()

font.name = 'SimSun' # 指定「宋體」

style.font = font

sheet.write(0,1,'test text')

#sheet.write(1,2,'excel讀寫測試',style)

sheet.write(1,2,'excel讀寫測試',xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;')

)

#之後,就可以保存文件(這里不需要想打開文件一樣需要close文件):

wbk.save('test4.xls')
請看代碼示例二:
## Startup Code Begin
import xlwt
from xlwt import *
import string
import os
from datetime import datetime
import time
from time import gmtime, strftime
current_date = strftime("%b %d %Y (%H:%M)")
## Startup Code End

## Moles Begin
def _size_col(sheet, col):
return sheet.col_width(col)

def _size_row(sheet, row):
return sheet.row_height(row)
## Moles End

## Style variable Begin
tittle_style = xlwt.easyxf('font: height 400, name Arial Black, colour_index blue, bold on; align: wrap on, vert centre, horiz center;' "borders: top double, bottom double, left double, right double;")
subtittle_left_style = xlwt.easyxf('font: height 240, name Arial, colour_index brown, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double;")
subtittle_right_style = xlwt.easyxf('font: height 240, name Arial, colour_index brown, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, right double;")
subtittle_top_and_bottom_style = xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;")
blank_style = xlwt.easyxf('font: height 650, name Arial, colour_index brown, bold off; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double, right double;")
normal_style = xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double, right double;")
## Style variable End

## Mole Add Begin
def print_blank_line(A, B, C, D):
ws.write_merge(A, B, C, D, "", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;'))
## Mole Add End

## Variable Begin
Excel = xlwt.Workbook()
## Variable End

## Sheet Name Begin
ws = Excel.add_sheet('Test Sheet')
## Sheet Name End

## Column Width Determine Begin
for A in range(100):
ws.col(A).width = 600
## Column Width Determine Begin

## Tittle Picture Begin
ws.write_merge(0, 0, 0, 42, "", xlwt.easyxf('font: height 700, name Arial, colour_index brown, bold off; align: wrap on, vert centre, horiz left;'))
##ws.insert_bitmap('test.bmp', 0, 0, 60, 5, 1.75, 1)
## Tittle Picture End

## Subtittle Write Begin
# Variable Begin
job_name = "Nicolas_Parametric"
fab_name = "Nicolas_Parametric"
printed_by = "Nic"
printed_date = current_date
# Variable End

border_type = ["dashed", "double", ""]

ws.write_merge(1, 1, 0, 6, "Job Name:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double;"))
ws.write_merge(1, 1, 7, 21, job_name, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))
ws.write_merge(2, 2, 0, 6, "Fab Name:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double;"))
ws.write_merge(2, 2, 7, 21, fab_name, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))

ws.write_merge(1, 1, 22, 27, "Printed By:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))
ws.write_merge(1, 1, 28, 42, printed_by, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, right double;"))
ws.write_merge(2, 2, 22, 27, "Printed Date:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))
ws.write_merge(2, 2, 28, 42, printed_date, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, right double;"))

print_blank_line(3, 3, 0, 42)
ws.write_merge(4, 4, 0, 42, "Report Name", xlwt.easyxf('font: height 400, name Arial, colour_index red, bold on, italic on, underline on; align: wrap on, vert centre, horiz center;'))
print_blank_line(5, 5, 0, 42)

ws.write_merge(6, 6, 0, 1, "#", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double, right double;"))
ws.write_merge(6, 6, 2, 7, "Piecemark", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 8, 10, "Qty", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 11, 20, "Size", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 21, 34, "Status", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 35, 42, "Date", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
## Subtittle Write End

normal_font = xlwt.easyxf('font: height 240, name Arial, colour_index black; align: wrap on, vert centre, horiz left;' "borders: top dashed, bottom dashed, left double, right double;")

for A in range(98):
ws.write_merge(7 + A, 7 + A, 0, 1, "", normal_font)
ws.write_merge(7 + A, 7 + A, 2, 7, "", normal_font)
ws.write_merge(7 + A, 7 + A, 8, 10, "", normal_font)
ws.write_merge(7 + A, 7 + A, 11, 20, "", normal_font)
ws.write_merge(7 + A, 7 + A, 21, 34, "", normal_font)
ws.write_merge(7 + A, 7 + A, 35, 42, "", normal_font)

## File Save As Begin
Excel.save('Test.xls')
## File Save As End

#### Start File Begin
##os.startfile("C:/Documents and Settings/dev01/Desktop/Test.xls")
#### Start File End

㈤ python xlwt寫入到表格里,會覆蓋之前的內容,怎麼做不覆蓋之前的內容

import xlrd

from xlutils. import

向已存在Excel中添加sheet:

#打開需要操作的excel表

wb=xlrd.open_workbook(path)

#復制原有表

newb=(wb)

#新增sheet

wbsheet=newb.add_sheet(「sheet名」)

向已存在sheet中添加行

#獲取原有excel表中名為『table'的sheet

tabsheet = newb.get_sheet('table')

#k表示該sheet的最後一行

k=len(tabsheet.rows)

#在原有sheet後面新增數據

tabsheet.write(k,0,data1)

tabsheet.write(k,1,data2)

tabsheet.write(k,2,data3)

㈥ xlwt支持python3.x嗎

支持,不過openpyxl更方便

㈦ PYTHON使用XLWT3保存EXCEL 出現AttributeError: 'bytes' object has no attribute 'encode'錯誤

把C:\Python34\lib\site-packages\xlwt3\biffrecords.py文件第235行中的uowner.encode('utf-8')改成uowner即可。
xlwt3有bug的。

㈧ Python 使用xlwt和xlrd操作Excel自動換sheet的問題,如何讓每5條數據放到一個sheet裡面

這個過程有幾個比較麻煩的問題,比如讀取日期、讀合並單元格內容。下面先看看基本的操作:
首先讀一個excel文件,有兩個sheet,測試用第二個sheet,sheet2內容如下:

python 對 excel基本的操作如下:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# -*- coding: utf-8 -*-
import xlrd
import xlwt
from datetime import date,datetime

def read_excel():
# 打開文件
workbook = xlrd.open_workbook(r'F:\demo.xlsx')
# 獲取所有sheet
print workbook.sheet_names() # [u'sheet1', u'sheet2']
sheet2_name = workbook.sheet_names()[1]

# 根據sheet索引或者名稱獲取sheet內容
sheet2 = workbook.sheet_by_index(1) # sheet索引從0開始
sheet2 = workbook.sheet_by_name('sheet2')

# sheet的名稱,行數,列數
print sheet2.name,sheet2.nrows,sheet2.ncols

# 獲取整行和整列的值(數組)
rows = sheet2.row_values(3) # 獲取第四行內容
cols = sheet2.col_values(2) # 獲取第三列內容

㈨ Python語言中的第三方模塊xlrd和xlwt是分別起讀取excel的內容和寫入excel的內容的作用

你這里存在一個誤區,只靠xlrd和xlwt不能直接實現對已經存在Excel進行數據插入,原因如下:

1、使用xlrd的xlrd.open_workbook()方法打開文件返回的是一個只讀對象,只能讀Excel內容,不能向Excel寫入數據;

2、使用xlwt的xlwt.Workbook()方法是創建一個空的Excel對象,然後向裡面寫入;

所以讀取和創建Excel都可以直接實現,但是修改已經存在的Excel需要藉助xlutils,它的作用是在xlrd類型的workbook和xlwt類型的workbook之間建立了一個管道,利用xlutils的方法將只讀對象變成可寫對象,簡單代碼實現如下:

importxlrd
fromxlutils.import

readOnlyWorkbook=xlrd.open_workbook('TestFile.xls')
readOnlySheet=readOnlyWorkbook.sheet_by_index(0)#通過sheet_by_index()獲取的sheet沒有write()方法

writableWorkbook=(readOnlyWorkbook)#利用xlutils.函數,將xlrd.Book轉為xlwt.Workbook
writableSheet=writableWorkbook.get_sheet(0)#通過get_sheet()獲取的sheet有write()方法
writableSheet.write(3,3,'insertData')#寫入3,3這個單元格

writableWorkbook.save('TestFile.xls')#保存文件

你自己增加代碼判斷原Excel某個單元格是否有數據,完善你的代碼,避免插入時覆蓋了,運行後Excel情況見截圖

上述方法我寫的是不能「直接」實現已有Excel的數據插入或修改,但是可以間接的來

  1. 使用xlrd讀原有Excel

  2. 使用xlwt創建一個新文件並把1中讀取的內容寫入

  3. 再插入需要增加的數據,保存

  4. 刪掉原Excel,把新創建的Excel改成原Excel的名字

這種方法你就自己玩,因為原來Excel可能有格式啊,公式啊什麼的,新創建就會很麻煩

熱點內容
sqlbrowser服務 發布:2025-02-02 01:32:14 瀏覽:730
jeecg源碼下載 發布:2025-02-02 01:08:47 瀏覽:365
多台電腦能共用一個伺服器嗎 發布:2025-02-02 01:08:44 瀏覽:26
oppo雲存儲空間怎麼清理 發布:2025-02-02 00:59:31 瀏覽:263
項目編譯啥意思 發布:2025-02-02 00:25:13 瀏覽:223
逐鹿中原怎樣做掛機腳本 發布:2025-02-02 00:23:39 瀏覽:28
安卓系統跟蹤在哪裡 發布:2025-02-02 00:23:38 瀏覽:899
安卓uc瀏覽器怎麼安裝油猴 發布:2025-02-02 00:23:38 瀏覽:299
中商情報網資料庫 發布:2025-02-02 00:09:26 瀏覽:533
python獲取目錄下所有文件名 發布:2025-02-01 23:47:54 瀏覽:896