1.写入行列值
import xlwt
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('My Worksheet')
# 写入excel
# 参数对应 行, 列, 值
worksheet.write(1, 0, label = 'this is test')
# 保存
workbook.save('Excel_test.xls')
2.更改样式
import xlwt
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet')
style = xlwt.XFStyle() # 初始化样式
font = xlwt.Font() # 为样式创建字体
font.name = 'Times New Roman'
font.bold = True # 黑体
font.underline = True # 下划线
font.italic = True # 斜体字
style.font = font # 设定样式
worksheet.write(0, 0, 'Unformatted value') # 不带样式的写入
worksheet.write(1, 0, 'Formatted value', style) # 带样式的写入
workbook.save('formatting.xls') # 保存文件
3.设置单元格宽度
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write(0, 0,'My Cell Contents')
# 设置单元格宽度
worksheet.col(0).width = 5555
workbook.save('cell_width.xls')
4.输入日期进表格
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')
5.合并行列单元格
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
# # 0 0 行 0 3 列
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
# 1 2 行 0 3 列
worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style)
workbook.save('Excel_Workbook.xls')
6.设置单元格对齐方式
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)
worksheet.col(0).width = 5555
workbook.save('Excel_Workbook.xls')
7.为单元格设置背景色
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')
8.为单元格设置边框
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
borders = xlwt.Borders() # Create 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
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')
案例1:
import xlwt
from xlwt import Workbook
def simulate_data():
cell_id_sets = [{(0, 1), (0, 0), (0, 5), (0, 4), (0, 3), (0, 2)}, {(7, 4), (7, 3), (7, 5)}, {(3, 0), (2, 0), (5, 0), (4, 0)}, {(2, 3), (3, 3), (4, 3), (5, 3)}, {(1, 0)}, {(6, 0)}, {(7, 0)}, {(1, 1)}, {(2, 1)}, {(3, 1)}, {(4, 1)}, {(5, 1)}, {(6, 1)}, {(7, 1)}, {(1, 2)}, {(2, 2)}, {(3, 2)}, {(4, 2)}, {(5, 2)}, {(6, 2)}, {(7, 2)}, {(1, 3)}, {(6, 3)}, {(1, 4)}, {(2, 4)}, {(3, 4)}, {(4, 4)}, {(5, 4)}, {(6, 4)}, {(1, 5)}, {(2, 5)}, {(3, 5)}, {(4, 5)}, {(5, 5)}, {(6, 5)}, set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
x_grids = [25, 187, 326, 434, 584, 713, 819]
y_grids = [12, 42, 108, 142, 176, 210, 244, 278, 312]
ids = 35
rst = []
for id in range(ids):
if len(cell_id_sets[id]) == 0:
continue
if len(cell_id_sets[id]) == 1:
cell = {}
cell_row, cell_col = list(cell_id_sets[id])[0]
cell["id"] = id
cell["row_start"] = cell_row # 结构坐标
cell["col_start"] = cell_col
cell["row_end"] = cell_row + 1
cell["col_end"] = cell_col + 1
cell["x1"] = x_grids[cell_col] # 绝对坐标
cell["y1"] = y_grids[cell_row]
cell["x2"] = x_grids[cell_col + 1]
cell["y2"] = y_grids[cell_row + 1]
cell["crnn"] = [] # 后续使用
cell["text"] = "" # 后续使用
rst.append(cell)
else:
id_min = sorted(cell_id_sets[id])[0]
id_max = sorted(cell_id_sets[id])[-1]
cell = {}
cell_row_min, cell_col_min = id_min
cell_row_max, cell_col_max = id_max
cell["id"] = id
cell["row_start"] = cell_row_min # 结构坐标
cell["col_start"] = cell_col_min
cell["row_end"] = cell_row_max + 1
cell["col_end"] = cell_col_max + 1
cell["x1"] = x_grids[cell_col_min] # 绝对坐标
cell["y1"] = y_grids[cell_row_min]
cell["x2"] = x_grids[cell_col_max + 1]
cell["y2"] = y_grids[cell_row_max + 1]
cell["crnn"] = [] # 后续使用
cell["text"] = "" # 后续使用
rst.append(cell)
return rst
# def str_cnt_ascii(str):
# cnt = 0
# for c in str:
# if c <= "~": # 是否准确?
# cnt += 1
# return cnt
#
#
# def clac_str_width(str):
# """
# 计算字符最大宽度, 考虑换行和中英文的情况
# 记中文宽度为英文宽度的2倍
# """
# return max([2 * (len(t)) - str_cnt_ascii(t) for t in str.split('\n')])
def debug_test_cell_excel():
rst = simulate_data()#得到模拟数据
print('rst:', rst)
wb = Workbook()
sheet1 = wb.add_sheet('Sheet 1', cell_overwrite_ok=True)
style = xlwt.easyxf(
'align: wrap on, vert centre, horiz center') # 设置行内换行, 水平竖直居中
for b in rst:
row_start = b['row_start']
col_start = b['col_start']
row_end = b['row_end']
col_end = b['col_end']
text = b['text']
if (col_end - col_start == 1) and (row_end - row_start == 1):
# # 自动调整列宽, 只对单个单元格(不计算合并单元格的内容情况)
# cwidth = sheet1.col(col_start).width
# column_txt_width = clac_str_width(text)
# if (column_txt_width * 367) > cwidth:
# sheet1.col(col_start).width = (
# column_txt_width * 367) # (Modify column width to match biggest data in that column)
sheet1.write(row_start, col_start, text, style)
else:
sheet1.write_merge(row_start, row_end - 1,
col_start, col_end - 1, text, style)
wb.save("table_test.xls")
if __name__ == '__main__':
debug_test_cell_excel()
案例2:
import json
def table_save_excel(table_boxes, save_name):
'''
表格识别的内容存excel
:param table_boxes:
:param save_name:
:return:
'''
# Writing to an excel
# sheet using Python
def str_cnt_ascii(str):
cnt = 0
for c in str:
if c <= "~": # 是否准确?
cnt += 1
return cnt
def clac_str_width(str):
"""
计算字符最大宽度, 考虑换行和中英文的情况
记中文宽度为英文宽度的2倍
"""
return max([2 * (len(t)) - str_cnt_ascii(t) for t in str.split('\n')])
import xlwt
from xlwt import Workbook
# Workbook is created
wb = Workbook()
# add_sheet is used to create sheet.
sheet1 = wb.add_sheet('Sheet 1', cell_overwrite_ok=True)# todo 不添加覆盖操作有bug, 找出bug来源
style = xlwt.easyxf('align: wrap on, vert centre, horiz center') # 设置行内换行, 水平竖直居中
for b in table_boxes:
print('b:', b)
row_start = b['row_start']
col_start = b['col_start']
row_end = b['row_end']
col_end = b['col_end']
text = b['text']
if (col_end - col_start == 1) and (row_end - row_start == 1):
# 自动调整列宽, 只对单个单元格(不计算合并单元格的内容情况)
cwidth = sheet1.col(col_start).width
column_txt_width = clac_str_width(text)
if (column_txt_width * 367) > cwidth:
sheet1.col(col_start).width = (
column_txt_width * 367) # (Modify column width to match biggest data in that column)
sheet1.write(row_start, col_start, text, style)
else:
sheet1.write_merge(row_start, row_end-1, col_start, col_end-1, text, style)
wb.save(save_name)
if __name__ == '__main__':
# path = './json规范/json示意.txt'
# with open(path, 'r', encoding='utf-8') as file:
# for i, read_info in enumerate(file.readlines()):
# print('read_info:', read_info)
#其中row_start代表横线的开始,row_end代表横线的结束,col_start代表列线的开始,col_end代表列线的结束,'text'代表识别结果,x1,y1,x2,y2代表在图片中坐标,crnn准备放概率值
table_boxes = [{'id': 0, 'row_start': 0, 'col_start': 0, 'row_end': 1, 'col_end': 6, 'x1': 121, 'y1': 472, 'x2': 833, 'y2': 499, 'crnn': [], 'text': '截止日期:2019年06月30日5单位:元'}, {'id': 1, 'row_start': 7, 'col_start': 0, 'row_end': 8, 'col_end': 6, 'x1': 121, 'y1': 711, 'x2': 833, 'y2': 740, 'crnn': [], 'text': '除以上所列结存或结欠贵公司之款额,本公司并无其他结存或结欠贵公司之款额。'}, {'id': 2, 'row_start': 2, 'col_start': 0, 'row_end': 6, 'col_end': 1, 'x1': 121, 'y1': 557, 'x2': 266, 'y2': 681, 'crnn': [], 'text': '应收贵公司金额\n应收账款\n其他应收款\n应收票据'}, {'id': 3, 'row_start': 2, 'col_start': 3, 'row_end': 6, 'col_end': 4, 'x1': 487, 'y1': 557, 'x2': 622, 'y2': 681, 'crnn': [], 'text': '应付贵公司金额\n应付账款\n其他应付款\n应付票据'}, {'id': 4, 'row_start': 1, 'col_start': 0, 'row_end': 2, 'col_end': 1, 'x1': 121, 'y1': 499, 'x2': 266, 'y2': 557, 'crnn': [], 'text': '项目'}, {'id': 5, 'row_start': 6, 'col_start': 0, 'row_end': 7, 'col_end': 1, 'x1': 121, 'y1': 681, 'x2': 266, 'y2': 711, 'crnn': [], 'text': '预付贵公司金额'}, {'id': 6, 'row_start': 1, 'col_start': 1, 'row_end': 2, 'col_end': 2, 'x1': 266, 'y1': 499, 'x2': 391, 'y2': 557, 'crnn': [], 'text': '本公司账面金\n额'}, {'id': 7, 'row_start': 2, 'col_start': 1, 'row_end': 3, 'col_end': 2, 'x1': 266, 'y1': 557, 'x2': 391, 'y2': 588, 'crnn': [], 'text': '\n'}, {'id': 8, 'row_start': 3, 'col_start': 1, 'row_end': 4, 'col_end': 2, 'x1': 266, 'y1': 588, 'x2': 391, 'y2': 619, 'crnn': [], 'text': '\n'}, {'id': 9, 'row_start': 4, 'col_start': 1, 'row_end': 5, 'col_end': 2, 'x1': 266, 'y1': 619, 'x2': 391, 'y2': 649, 'crnn': [], 'text': '\n'}, {'id': 10, 'row_start': 5, 'col_start': 1, 'row_end': 6, 'col_end': 2, 'x1': 266, 'y1': 649, 'x2': 391, 'y2': 681, 'crnn': [], 'text': '\n'}, {'id': 11, 'row_start': 6, 'col_start': 1, 'row_end': 7, 'col_end': 2, 'x1': 266, 'y1': 681, 'x2': 391, 'y2': 711, 'crnn': [], 'text': ''}, {'id': 12, 'row_start': 1, 'col_start': 2, 'row_end': 2, 'col_end': 3, 'x1': 391, 'y1': 499, 'x2': 487, 'y2': 557, 'crnn': [], 'text': '如金额不符,请\n列明贵公司账面\n记承的金额(注)'}, {'id': 13, 'row_start': 2, 'col_start': 2, 'row_end': 3, 'col_end': 3, 'x1': 391, 'y1': 557, 'x2': 487, 'y2': 588, 'crnn': [], 'text': ''}, {'id': 14, 'row_start': 3, 'col_start': 2, 'row_end': 4, 'col_end': 3, 'x1': 391, 'y1': 588, 'x2': 487, 'y2': 619, 'crnn': [], 'text': '\n'}, {'id': 15, 'row_start': 4, 'col_start': 2, 'row_end': 5, 'col_end': 3, 'x1': 391, 'y1': 619, 'x2': 487, 'y2': 649, 'crnn': [], 'text': ''}, {'id': 16, 'row_start': 5, 'col_start': 2, 'row_end': 6, 'col_end': 3, 'x1': 391, 'y1': 649, 'x2': 487, 'y2': 681, 'crnn': [], 'text': '\n'}, {'id': 17, 'row_start': 6, 'col_start': 2, 'row_end': 7, 'col_end': 3, 'x1': 391, 'y1': 681, 'x2': 487, 'y2': 711, 'crnn': [], 'text': '\n'}, {'id': 18, 'row_start': 1, 'col_start': 3, 'row_end': 2, 'col_end': 4, 'x1': 487, 'y1': 499, 'x2': 622, 'y2': 557, 'crnn': [], 'text': '项目'}, {'id': 19, 'row_start': 6, 'col_start': 3, 'row_end': 7, 'col_end': 4, 'x1': 487, 'y1': 681, 'x2': 622, 'y2': 711, 'crnn': [], 'text': '预收贵公司金额'}, {'id': 20, 'row_start': 1, 'col_start': 4, 'row_end': 2, 'col_end': 5, 'x1': 622, 'y1': 499, 'x2': 739, 'y2': 557, 'crnn': [], 'text': '本公司账面金\n额'}, {'id': 21, 'row_start': 2, 'col_start': 4, 'row_end': 3, 'col_end': 5, 'x1': 622, 'y1': 557, 'x2': 739, 'y2': 588, 'crnn': [], 'text': ''}, {'id': 22, 'row_start': 3, 'col_start': 4, 'row_end': 4, 'col_end': 5, 'x1': 622, 'y1': 588, 'x2': 739, 'y2': 619, 'crnn': [], 'text': '1,254,555.54'}, {'id': 23, 'row_start': 4, 'col_start': 4, 'row_end': 5, 'col_end': 5, 'x1': 622, 'y1': 619, 'x2': 739, 'y2': 649, 'crnn': [], 'text': '2,000.00'}, {'id': 24, 'row_start': 5, 'col_start': 4, 'row_end': 6, 'col_end': 5, 'x1': 622, 'y1': 649, 'x2': 739, 'y2': 681, 'crnn': [], 'text': ''}, {'id': 25, 'row_start': 6, 'col_start': 4, 'row_end': 7, 'col_end': 5, 'x1': 622, 'y1': 681, 'x2': 739, 'y2': 711, 'crnn': [], 'text': '\n'}, {'id': 26, 'row_start': 1, 'col_start': 5, 'row_end': 2, 'col_end': 6, 'x1': 739, 'y1': 499, 'x2': 833, 'y2': 557, 'crnn': [], 'text': '列明贵公司账\n面记录的金额\n(注)'}, {'id': 27, 'row_start': 2, 'col_start': 5, 'row_end': 3, 'col_end': 6, 'x1': 739, 'y1': 557, 'x2': 833, 'y2': 588, 'crnn': [], 'text': ''}, {'id': 28, 'row_start': 3, 'col_start': 5, 'row_end': 4, 'col_end': 6, 'x1': 739, 'y1': 588, 'x2': 833, 'y2': 619, 'crnn': [], 'text': ''}, {'id': 29, 'row_start': 4, 'col_start': 5, 'row_end': 5, 'col_end': 6, 'x1': 739, 'y1': 619, 'x2': 833, 'y2': 649, 'crnn': [], 'text': ''}, {'id': 30, 'row_start': 5, 'col_start': 5, 'row_end': 6, 'col_end': 6, 'x1': 739, 'y1': 649, 'x2': 833, 'y2': 681, 'crnn': [], 'text': '\n'}, {'id': 31, 'row_start': 6, 'col_start': 5, 'row_end': 7, 'col_end': 6, 'x1': 739, 'y1': 681, 'x2': 833, 'y2': 711, 'crnn': [], 'text': ''}]
print('table_boxes:', table_boxes)
save_name = './test.xls'
print('table_boxes[0]', table_boxes[0])
table_save_excel(table_boxes, save_name)