是否要更改此代码以使用python3在excel中编写?

时间:2019-03-21 04:04:31

标签: python excel csv

  

如何转换此代码以在excel中获得输出?

with open('table.csv', 'w', encoding='utf-8') as csvfile:
    wr = csv.writer(csvfile)
    for table in extracted_document['tables']:
         for row in table['rows']:
             wr.writerow([cell['content'] for cell in row['cells']])
  

我正在使用此代码在cvs文件中获取输出,但现在我想在excel中编写..如何更改此代码以在excel中获取输出?请帮助我

2 个答案:

答案 0 :(得分:0)

您可以使用xlwt模块。

import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('sheet1')
for table in extracted_document['tables']:
    for row_cnt, row in enumerate(table['rows']):
        for col_cnt, c in enumerate([cell['content'] for cell in row['cells']]):
            ws.write(row_cnt, col_cnt, c)
wb.save('table.xls')

答案 1 :(得分:0)

''' 创建工作簿 '''

import xlwt
workbook=xlwt.Workbook()
sheet=workbook.add_sheet("Sheet Name")

''' 写入单元格 '''

sheet.write(0,0,'foobar')#row ,column,value

''' 使用风格 '''

stype=xlwt.easyxf('font:bold 1')
sheet.write(0,0,'foobar',style)

'''