当前位置:首页 » 编程语言 » 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可能有格式啊,公式啊什么的,新创建就会很麻烦

热点内容
安卓什么软件测试手机电池 发布:2025-02-02 04:28:52 浏览:991
手机上传快 发布:2025-02-02 04:27:46 浏览:306
电脑配置详解图解都有哪些 发布:2025-02-02 04:26:27 浏览:713
景区应该有什么配置 发布:2025-02-02 04:09:08 浏览:119
c语言与java工作 发布:2025-02-02 03:59:57 浏览:282
qq买什么不要支付密码 发布:2025-02-02 03:50:29 浏览:495
android读取视频 发布:2025-02-02 03:46:57 浏览:826
手机号序列码的密码在哪里 发布:2025-02-02 03:29:34 浏览:878
安卓怎么换回鸿蒙系统 发布:2025-02-02 03:24:35 浏览:513
完美国际邻水镇箱子密码是多少 发布:2025-02-02 03:17:04 浏览:625