美琪 發表於 2019-12-5 17:41:00

python的xlwt模块

<p><strong>xlwt模块用来设置xls。</strong></p>
<h2 id="一安装此处用的是python3解析器">一、安装(此处用的是python3解析器)</h2>
<p>pip3 install xlwt</p>
<h2 id="二设置字体">二、设置字体</h2>
<pre><code class="language-python">import xlwt

# 创建一个工作簿
xl = xlwt.Workbook(encoding='utf-8')
# 创建一个sheet对象,第二个参数是指单元格是否允许重设置,默认为False
sheet = xl.add_sheet('菜鸟的成长历程', cell_overwrite_ok=False)
# 初始化样式
style = xlwt.XFStyle()
# 为样式创建字体
font = xlwt.Font()
font.name = 'Times New Roman'
# 黑体
font.bold = True
# 下划线
font.underline = False
# 斜体字
font.italic = True
# 设定样式
style.font = font
# 第一个参数代表行,第二个参数是列,第三个参数是内容,第四个参数是格式
sheet.write(0, 0, '不带样式的携入')
sheet.write(1, 0, '带样式的写入', style)

# 保存文件
xl.save('C:\\Users\\15136\\Desktop\\字体.xls')
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205145304061-2125637082.png"></p>
<h2 id="三设置单元格宽度">三、设置单元格宽度</h2>
<pre><code class="language-python">import xlwt
xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('单元格宽度', cell_overwrite_ok=True)
sheet.write(0, 0, '第一个行第一列内容')
sheet.col(0).width = 5000
xl.save('C:\\Users\\15136\\Desktop\\单元格宽度.xls')
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205150354488-18674081.png"></p>
<h2 id="四将一个日期输入一个单元格">四、将一个日期输入一个单元格</h2>
<pre><code class="language-python">import xlwt
import datetime
xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('日期', cell_overwrite_ok=True)
# 初始化样式
style = xlwt.XFStyle()
#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, :mm:ss, mm:ss.0
style.num_format_str = 'M/D/YY'
sheet.write(0, 0, datetime.date.today(), style)
sheet.col(0).width = 5000
xl.save('C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls')
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205151151552-664562076.png"></p>
<h2 id="五添加一个公式">五、添加一个公式</h2>
<pre><code class="language-python">import xlwt
xl = xlwt.Workbook(encoding='UTF-8')
sheet = xl.add_sheet('公式', cell_overwrite_ok=True)
# 第一行第一列
sheet.write(0, 0, 5)
# 第一行第二列
sheet.write(0, 1, 3)
sheet.write(0,2,xlwt.Formula('A1 * B1'))
sheet.write(1,0,xlwt.Formula("A1 + B1"))
xl.save("C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls")
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205152441053-1926910542.png"></p>
<h2 id="六在单元格中添加超链接">六、在单元格中添加超链接</h2>
<pre><code class="language-python">import xlwt

xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('超链接')
sheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")'))
xl.save("C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls")
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205153701886-1687970674.png"></p>
<h2 id="七合并列和行">七、合并列和行</h2>
<pre><code class="language-python">import xlwt
xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('合并列和行')
# (行,行,列,列,内容)
sheet.write_merge(0, 1, 0, 3, '合并从第一行到第二行,第一列到第四列')
font = xlwt.Font()# 创建字体示例
font.bold = True# bold设置为黑体字
style = xlwt.XFStyle()# 初始化样式
style.font = font
sheet.write_merge(2, 3, 0, 3, '合并从第三行到第四行,第一列到第四列', style)
xl.save('C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls')
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205162652502-404577901.png"></p>
<h2 id="八单元格的对齐方式">八、单元格的对齐方式</h2>
<pre><code class="language-python">import xlwt
xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('单元格的对齐方式')
# 创建对齐格式对象
alignment = xlwt.Alignment()
# 左右的对其,水平居中 May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER,
# HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED,
# HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.horz = xlwt.Alignment.HORZ_CENTER
# 上下对齐 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
style = xlwt.XFStyle()# 创建一个样式对象
style.alignment = alignment# 将格式Alignment对象加入到样式对象
sheet.write(0,0,'单元居中',style)
xl.save('C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls')
</code></pre>
<h1 id="alignment-n-结盟-队列排成直线-校直调整-工-准线">Alignment n. 结盟; 队列,排成直线; 校直,调整; [工] 准线;</h1>
<h1 id="horz-水平的地平式">HORZ 水平的;地平式;</h1>
<h1 id="general-普遍的-大致的-综合的-总的全体的">GENERAL 普遍的; 大致的; 综合的; 总的,全体的;</h1>
<h1 id="filled-满的-填满的-充气的-加载的">FILLED 满的; 填满的; 充气的; 加载的;</h1>
<h1 id="justified-adj-有正当理由的合理的-事出有因的">JUSTIFIED adj. 有正当理由的,合理的; 事出有因的;</h1>
<h1 id="distributed-分布式的">DISTRIBUTED 分布式的;</h1>
<h1 id="vert-n-森林中的草木绿色-倾侧-倾转">VERT n. 森林中的草木,绿色; 倾侧; 倾转;</h1>
<p><img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205165528590-1588189458.png"></p>
<h2 id="九为单元格加边框">九、为单元格加边框</h2>
<pre><code class="language-python">import xlwt
xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('单元格添加边框')
# 创建边框对象
borders = xlwt.Borders()
borders.left = xlwt.Borders.DASHED# 对边框对象进行操作,指定边框上下左右的边框类型为虚线
# DASHED虚线
# NO_LINE没有
# THIN实线
# 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      #指定上下左右的边框颜色为0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style = xlwt.XFStyle()# Create Style   #创建样式对象
style.borders = borders# 将设置好的边框对象borders 加到样式对象style中。
sheet.write(0, 0, '单元格内容', style)
sheet.write(1, 0, '单元格内容', style)
sheet.write(0, 1, '单元格内容', style)
sheet.write(1, 1, '单元格内容', style)
sheet.col(0).wigth = 4000
sheet.col(1).wigth = 4000
xl.save('C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls')
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205171459540-1089364250.png"></p>
<h2 id="十设置背景色">十、设置背景色</h2>
<pre><code class="language-python">import xlwt
xl = xlwt.Workbook(encoding='utf-8')
sheet = xl.add_sheet('单元格背景色')
# 创建背景模式对像
pattern = xlwt.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
sheet.write(0, 0, '单元格内容', style)#向单元格写入内容时使用样式对象style
xl.save('C:\\Users\\15136\\Desktop\\菜鸟的成长历程.xls')
</code></pre>
<p>结果:<br>
<img src="https://img2018.cnblogs.com/blog/1879367/201912/1879367-20191205174020782-303008044.png"></p>


</div>
<div id="MySignature" role="contentinfo">
    所有的事都会过去,我们所有的人都是从小白开始,坚持下去。<br><br>
来源:https://www.cnblogs.com/li-dy/p/11990729.html
頁: [1]
查看完整版本: python的xlwt模块