使用Python和xlwt向Excel文件中寫入中文

原文地址

       Python等工具確實是不錯的工具,但是有時候不管是基礎的Python還是Python的軟件包都讓我覺得對中文不是很親近。時不時地遇到一點問題很正常,剛剛在寫Excel文件的時候就又遇到了這樣的問題。

       爲了能夠說明情況,假設我想把當前文件夾中所有的文件名稱全都寫入到Excel文件中。當前的目錄信息如下:

grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ ls -l

total 1464

-rwxrwxrwx 1 rootroot  13067 Aug 20 18:58 as86彙編程序的編譯與鏈接.docx

-rwxrwxrwx 1 rootroot  13794 Aug  8 22:07 C語言中access函數的使用.docx

-rwxrwxrwx 1 rootroot  13285 Aug  8 23:08 C語言中合併字符串.docx

-rwxrwxrwx 1 rootroot  15514 Aug  8 23:28 C語言中的動態內存分配.docx

-rwxrwxrwx 1 rootroot  13765 Aug  8 23:43 GNU glibc中對C語言標準庫中動態內存管理的改進.docx

-rwxrwxrwx 1 rootroot  14450 Aug  8 22:37 GNU glibc庫中函數執行成功與失敗的返回值.docx

-rwxrwxrwx 1 rootroot  13485 Aug 20 14:51 Linux 0.12的任務調度猜測.docx

-rwxrwxrwx 1 rootroot  11934 Aug 20 13:20 Linux BIOS啓動.docx

-rwxrwxrwx 1 rootroot  13349 Aug 20 19:38 Linux中dd命令的使用.docx

-rwxrwxrwx 1 rootroot  13802 Aug 20 19:20 Linux中使用dd命令修改as86彙編編譯鏈接後的程序.docx

-rwxrwxrwx 1 rootroot  15118 Aug 20 16:47 Linux啓動代碼boot.s理解.docx

-rwxrwxrwx 1 rootroot  16005 Aug 20 14:25 Linux操作系統的BIOS啓動流程.docx

-rwxrwxrwx 1 rootroot  14755 Aug 20 01:29 Linux的proc文件系統.docx

-rwxrwxrwx 1 rootroot  12777 Aug  1 23:50 mot文件的單行校驗與全局校驗.docx

-rwxrwxrwx 1 rootroot 131404 Aug 13 14:50 Office 2016 Excel實現下拉欄.docx

-rwxrwxrwx 1 rootroot  86665 Aug 19 11:44 Office 2016中修改正文默認字體.docx

-rwxrwxrwx 1 rootroot 113476 Aug  9 21:07 PPT中修改已插入對象的圖標.docx

-rwxrwxrwx 1 rootroot  13636 Aug 16 22:23 Python2與Python3中__bool__方法的差異.docx

-rwxrwxrwx 1 rootroot  13924 Aug  1 23:32 Python2與Python3中除法功能的異同.docx

-rwxrwxrwx 1 rootroot  14551 Aug 16 23:09 Python2與Python3在不同類的對象比較上的差異.docx

-rwxrwxrwx 1 rootroot  14101 Aug 12 10:30 Python abc模塊的幾個小知識點.docx

-rwxrwxrwx 1 rootroot  13581 Aug  9 23:24 Python OOP容器設計時合理引入列表方法.docx

-rwxrwxrwx 1 rootroot  12890 Aug 12 14:29 Python中isinstance用法.docx

-rwxrwxrwx 1 rootroot  13899 Aug  9 22:46 Python中list的extend方法.docx

-rwxrwxrwx 1 rootroot  13142 Aug 18 02:22 Python中__new__方法的使用.docx

-rwxrwxrwx 1 rootroot  13280 Aug 20 23:17 Python中使用property實現類的特性.docx

-rwxrwxrwx 1 rootroot  13257 Aug 16 22:39 Python中定製類的比較運算方法.docx

-rwxrwxrwx 1 rootroot  13897 Aug 18 22:45 Python中最基礎類的屬性和信息.docx

-rwxrwxrwx 1 rootroot  13567 Aug 19 11:53 Python中的@classmethod用法.docx

-rwxrwxrwx 1 rootroot  14859 Aug 12 15:32 Python中的join函數用法.docx

-rwxrwxrwx 1 rootroot  13832 Aug 18 01:38 Python中的weakref簡單小結.docx

-rwxrwxrwx 1 rootroot  13314 Aug 16 20:51 Python中的哈希常識小結.docx

-rwxrwxrwx 1 rootroot  13822 Aug 12 19:15 Python中的靜態函數用法.docx

-rwxrwxrwx 1 rootroot  15223 Aug 11 00:12 Python中@符號的用法-1.docx

-rwxrwxrwx 1 rootroot    162 Aug 21 23:20 ~$Python和xlwt向Excel文件中寫入中文.docx

-rwxrwxrwx 1 rootroot  13519 Aug 10 20:01 Python實現接受任意個數參數的函數.docx

-rwxrwxrwx 1 rootroot  15114 Aug 19 20:31 Python實現更相減損術求解最大公約數.docx

-rwxrwxrwx 1 rootroot  13425 Aug 10 22:51 Python編程中NotImplementedError的使用.docx

-rwxrwxrwx 1 rootroot  13345 Aug 20 22:27 Python通過屬性手段實現只允許調用一次的方法.docx

-rwxrwxrwx 1 rootroot  13586 Aug 19 11:15 Python面向對象編程中屬性的基本操作.docx

-rwxrwxrwx 1 rootroot 139264 Aug  2 23:50 Simulink解析帶有係數和偏移量的CAN報文信息.docx

-rwxrwxrwx 1 rootroot  13542 Aug 19 17:22 TAOCP中1.1習題1解答與思考.docx

-rwxrwxrwx 1 rootroot  13683 Aug 19 16:05 TAOCP中最大公約數算法理解.docx

-rwxrwxrwx 1 rootroot  13539 Aug 20 18:41 Ubuntu上安裝as86彙編器.docx

-rwxrwxrwx 1 rootroot 203890 Aug 12 14:20 Windows10安裝bash.docx

-rwxrwxrwx 1 rootroot    258 Aug 21 23:19 xlwt_demo.py

-rwxrwxrwx 1 rootroot  13353 Aug 18 02:07 使用__del__方法在Python文件處理時保證文件關閉.docx

-rwxrwxrwx 1 rootroot 127142 Aug  7 21:07 使用PolySpace進行模型的檢測.docx

-rwxrwxrwx 1 rootroot  12430 Aug 21 23:22 使用Python和xlwt向Excel文件中寫入中文.docx

-rwxrwxrwx 1 rootroot  13303 Aug 19 19:06 輾轉相除法求最大公約數.docx

       其中,xlwt_demo.py是提前編寫Python腳本,代碼如下:

#!/usr/bin/python

# -*- coding-cp936-*-

 

import os

import xlwt

from xlwt importWorkbook

 

book = Workbook()

sheet1 =book.add_sheet('list')

row_num = 0

for f inos.listdir('.'):

       sheet1.write(row_num,0,f)

       row_num += 1

book.save('demo.xls')

       程序運行結果:

grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ python xlwt_demo.py

Traceback (mostrecent call last):

  File "xlwt_demo.py", line 14, in<module>

    book.save('demo.xls')

  File"/usr/local/lib/python2.7/dist-packages/xlwt/Workbook.py", line 710,in save

    doc.save(filename_or_stream,self.get_biff_data())

  File"/usr/local/lib/python2.7/dist-packages/xlwt/Workbook.py", line 674,in get_biff_data

    shared_str_table   = self.__sst_rec()

  File"/usr/local/lib/python2.7/dist-packages/xlwt/Workbook.py", line 636,in __sst_rec

    return self.__sst.get_biff_record()

  File"/usr/local/lib/python2.7/dist-packages/xlwt/BIFFRecords.py", line77, in get_biff_record

    self._add_to_sst(s)

  File"/usr/local/lib/python2.7/dist-packages/xlwt/BIFFRecords.py", line92, in _add_to_sst

    u_str = upack2(s, self.encoding)

  File"/usr/local/lib/python2.7/dist-packages/xlwt/UnicodeUtils.py", line50, in upack2

    us = unicode(s, encoding)

UnicodeDecodeError:'ascii' codec can't decode byte 0xe6 in position 4: ordinal not in range(128)

       從上面的提示可以看出,這個工具默認支持的是ASCII碼。即使是我在代碼的開頭指定了編碼的格式,依然遇到了問題。如果是這樣,那麼肯定是xlwt有專門的用法。經過查看文檔,修改代碼如下:

#!/usr/bin/python

# -*- coding:cp936 -*-

 

import os

import xlwt

from xlwt importWorkbook

 

book =Workbook(encoding='utf-8')

sheet1 =book.add_sheet('list')

row_num = 0

for f inos.listdir('.'):

       sheet1.write(row_num,0,f)

       row_num += 1

book.save('demo.xls')

       程序執行結果如下:

grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ python xlwt_demo.py

grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ ls

as86彙編程序的編譯與鏈接.docx                       Python中使用property實現類的特性.docx

C語言中access函數的使用.docx                        Python中定製類的比較運算方法.docx

C語言中合併字符串.docx                             Python中最基礎類的屬性和信息.docx

C語言中的動態內存分配.docx                         Python中的@classmethod用法.docx

demo.xls                                            Python中的join函數用法.docx

GNU  glibc中對C語言標準庫中動態內存管理的改進.docx  Python中的weakref簡單小結.docx

GNU glibc庫中函數執行成功與失敗的返回值.docx        Python中的哈希常識小結.docx

Linux 0.12的任務調度猜測.docx                      Python中的靜態函數用法.docx

Linux BIOS啓動.docx                                Python中@符號的用法-1.docx

Linux中dd命令的使用.docx                            ~$Python和xlwt向Excel文件中寫入中文.docx

Linux中使用dd命令修改as86彙編編譯鏈接後的程序.docx  Python實現接受任意個數參數的函數.docx

Linux啓動代碼boot.s理解.docx                        Python實現更相減損術求解最大公約數.docx

Linux操作系統的BIOS啓動流程.docx                    Python編程中NotImplementedError的使用.docx

Linux的proc文件系統.docx                            Python通過屬性手段實現只允許調用一次的方法.docx

mot文件的單行校驗與全局校驗.docx                   Python面向對象編程中屬性的基本操作.docx

Office 2016 Excel實現下拉欄.docx                   Simulink解析帶有係數和偏移量的CAN報文信息.docx

Office 2016中修改正文默認字體.docx                  TAOCP中1.1習題1解答與思考.docx

PPT中修改已插入對象的圖標.docx                     TAOCP中最大公約數算法理解.docx

Python2與Python3中__bool__方法的差異.docx           Ubuntu上安裝as86彙編器.docx

Python2與Python3中除法功能的異同.docx               Windows10安裝bash.docx

Python2與Python3在不同類的對象比較上的差異.docx     xlwt_demo.py

Python abc模塊的幾個小知識點.docx                   使用__del__方法在Python文件處理時保證文件關閉.docx

Python OOP容器設計時合理引入列表方法.docx           使用PolySpace進行模型的檢測.docx

Python中isinstance用法.docx                         使用Python和xlwt向Excel文件中寫入中文.docx

Python中list的extend方法.docx                       輾轉相除法求最大公約數.docx

Python中__new__方法的使用.docx

       Excel文件已經生成,具體的內容如下:


       漢字寫入成功。

       值得注意的是,在Windows下編碼cp936是可以寫入成功的,但是在Linux中只能夠用utf-8。這是個很有意思的現象,目前我還沒能夠想明白。

發佈了60 篇原創文章 · 獲贊 52 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章