乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      使用python操作Excel——xlrd、xlwt、xlutils庫

       Four兄 2019-09-03

      一、使用xlrd進行讀操作

      打開workbook

      1. import xlrd
      2. wb = xlrd.open_workbook('mywokbook.xls')

      檢查表單名字

      wb.sheet_names()

      獲取sheet表

      1. sh = wb.sheet_by_index(0) # 索引獲取
      2. sh = wb.sheet_by_name('Sheet01') # 名字獲取

      遞歸打印所有行數(shù)據(jù)

      1. for n in range(sh.nrows):
      2. print sh.row_values(n)

      返回第N列數(shù)據(jù)

      first_column = sh.col_values(N)

      通過索引讀取某單元格數(shù)據(jù)

      cell_A1 = sh.cell(0, 0).value

      二、使用xlwt進行寫操作

      初始化workbook對象,之后才能進行寫入操作

      1. import xlwt
      2. wbk = xlwt.Workbook()
      3. sheet = wbk.add_sheet('sheet2')

      寫入數(shù)據(jù)

      sheet.write(0, 1, 'new text')

      保存文件

      wbk.save('new.xls')

      注意:修改表單內容,需要使用cell_overwrite_ok=True來創(chuàng)建worksheet

      1. sheet2 = wbk.add_sheet('sheet2', cell_overwrite_ok=True)
      2. sheet2.write(0, 0, 'text')
      3. sheet2.write(0, 0, 'altertext')

      三、使用xlutils進行修改操作

      Python中一般使用xlrd(excel read)來讀取Excel文件,使用xlwt(excel write)來生成Excel文件(可以控制Excel中單元格的格式),需要注意的是,用xlrd讀 取excel是不能對其進行操作的:xlrd.open_workbook()方法返回xlrd.Book類型,是只讀的,不能對其進行操作。而 xlwt.Workbook()返回的xlwt.Workbook類型的save(filepath)方法可以保存excel文件。因此對于讀取和生成Excel文件都非常容易處理,但是對于已經存在的Excel文件進行修改就比較麻煩了。不過,還有一個xlutils(依賴于xlrd和xlwt)提供復制excel文件內容和修改文件的功能。其實際也只是在xlrd.Book和xlwt.Workbook之間建立了一個管道而已,如下圖:

      xlutils.copy模塊的copy()方法實現(xiàn)這個功能

      1. from xlrd import open_workbook
      2. from xlutils.copy import copy
      3. rb = open_workbook('D:\\text.xls')
      4. # 通過get_sheet()獲取的sheet才有write()方法
      5. wb = copy(rb)
      6. ws = wb.get_sheet(0)
      7. ws.write(0, 0, 'changed!')
      8. wb.save('D:\\new.xls')

      四、實際工作使用

      將公司采購單轉換成苗木平臺生成的Excel模板,采購單總共76條數(shù)據(jù),模板14種苗木分類。

      采購數(shù)據(jù)樣式:

      苗木平臺模板數(shù)據(jù)樣式:

      生成的Excel表格

      偷了個懶,比如時間數(shù)據(jù)的格式沒進行設置,實際代碼如下

      1. import re
      2. import xlrd
      3. import xlwt
      4. from xlutils.copy import copy
      5. # 打開sheet表
      6. def open_sheet(xl_name):
      7. xl = xlrd.open_workbook(xl_name)
      8. xl_sheet = xl.sheets()[0]
      9. return xl_sheet
      10. # 將原文件的采購行插入匹配模板的采購行
      11. def insert_mb(L, line_no, ws):
      12. for x in range(len(L)):
      13. one_tree = L[x]
      14. line_no += 1
      15. for i in range(mb_sheet.nrows):
      16. if i == 1:
      17. ws.write(line_no, i, tree_classify(one_tree[2]))
      18. if i == 2:
      19. ws.write(line_no, i, one_tree[2])
      20. if i == 3:
      21. ws.write(line_no, i, one_tree[7])
      22. if i == 4:
      23. ws.write(line_no, i, '43485')
      24. if i == 7:
      25. ws.write(line_no, i, tree_classify(str(one_tree[3])))
      26. if i == 8:
      27. ws.write(line_no, i, one_tree[4])
      28. if i == 9:
      29. ws.write(line_no, i, one_tree[5])
      30. if i == 12:
      31. ws.write(line_no, i, one_tree[-1])
      32. # 處理采購項名稱
      33. def tree_classify(tree_name):
      34. s = re.sub('[A-Z]', '', tree_name)
      35. s = s.split('(')[0]
      36. return s
      37. # 查找模板中苗木某種分類的行號
      38. def search_no(sheet, class_no):
      39. for i in range(len(col_data)):
      40. if class_no == 1 and col_data[i] == '一':
      41. line_no = i
      42. elif class_no == 2 and col_data[i] == '二':
      43. line_no = i
      44. elif class_no == 3 and col_data[i] == '三':
      45. line_no = i
      46. elif class_no == 4 and col_data[i] == '四':
      47. line_no = i
      48. elif class_no == 5 and col_data[i] == '五':
      49. line_no = i
      50. elif class_no == 6 and col_data[i] == '六':
      51. line_no = i
      52. return line_no
      53. # 讀取所有的采購項并按序號分類
      54. def load_class_no(class_no, sheet):
      55. L = []
      56. for i in range(4, sheet.nrows-6):
      57. one_tree = sheet.row_values(i)[:-8]
      58. if one_tree[0] == class_no:
      59. L.append(one_tree)
      60. return L
      61. # copy模板才可以修改
      62. def copy_alter(sheet):
      63. cp_sheet = copy(sheet)
      64. if __name__ == '__main__':
      65. order_name = '副本徑河項目苗木采購審批表.xlsx'
      66. mb_name = 'Copy of importBuyList_ex.xls'
      67. tree_sheet = open_sheet(order_name)
      68. mb_sheet = open_sheet(mb_name)
      69. # 模板中第一列的數(shù)據(jù)
      70. col_data = mb_sheet.col_values(0)
      71. # copy后模板才可以修改
      72. mb = xlrd.open_workbook(mb_name)
      73. cp_sheet = copy(mb)
      74. wr_sheet = cp_sheet.get_sheet(0)
      75. for no in range(1, 7):
      76. class_trees = load_class_no(no, tree_sheet)
      77. line_no = search_no(wr_sheet, no)
      78. insert_mb(class_trees, line_no, wr_sheet)
      79. cp_sheet.save('new.xls')

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多