python 批量生成excel xls文件

python3 读写Excel  xls文件

  • 2007版以前的Excel(xls结尾的),需要使用xlrd读,xlwt写
  • 2007版以后的Excel(xlsx结尾的),需要使用openpyxl来读写。

pypi的地址:

  • https://pypi.python.org/pypi/xlwt
  • https://pypi.python.org/pypi/xlrd
  • https://pypi.python.org/pypi/openpyxl
import xlwt


aoid_name_dict = {}
with open('aoid_name.txt', 'r', encoding='utf8')as f:
    for line in f:
        aoid, name = line.strip('\n').split('\t')
        wb = xlwt.Workbook(encoding='utf8')
        ws = wb.add_sheet('Sheet1')
        ws.write(0, 0, name)
        ws.write(0, 1, aoid)
        aoid_name_dict[aoid] = [name, wb, ws, 1]

with open('wpid_name_aoid.txt', 'r', encoding='utf8')as f:
    for line in f:
        wpid, name, aoid = line.strip('\r\n').split('\t')
        aoid_value = aoid_name_dict[aoid]
        ws, row_excel = aoid_value[2], aoid_value[3]
        ws.write(row_excel, 0, wpid)
        ws.write(row_excel, 1, name)
        row_excel += 1
        aoid_name_dict[aoid][3] = row_excel

for key in aoid_name_dict:
    wb = aoid_name_dict[key][1]
    name = aoid_name_dict[key][0]
    wb.save('./excel_xlss/' + name + '.xls')

 

你可能感兴趣的:(python,excel)