Writing Chinese to an Excel file using Python and XLWT

Source: Internet
Author: User
Below for everyone to share an example that uses Python and XLWT to write Chinese in Excel file, has the very good reference value, hoped to be helpful to everybody. Come and see it together.

Tools like Python are really good tools, but sometimes the basic Python or Python package makes me feel very close to Chinese. It's normal to meet a little bit of a problem every now and then, just when you're writing Excel files.

To be able to illustrate the situation, let's say I want to write all the file names in the current folder to an Excel file.

The current directory information is as follows:

GREY@DESKTOP-3T80NPQ:/MNT/E/01_WORKSPACE/01_DOCS/02_BLOG/2017 year/August $ ls-ltotal 1464-rwxrwxrwx 1 rootroot 13067 20 18:58 compilation and linking of As86 assembler. DOCX-RWXRWXRWX 1 rootroot 13794 8 22:07 The use of Access functions in the C language. DOCX-RWXRWXRWX 1 rootroot 13285 8 23: The merged string in the C language. DOCX-RWXRWXRWX 1 rootroot 15514 8 23:28 dynamic memory allocation in C. DOCX-RWXRWXRWX 1 rootroot 13765 8 23:43 GNU glibc The improvement of dynamic memory management in C language standard library. DOCX-RWXRWXRWX 1 rootroot 14450 8 22:37 The GNU glibc Library performs a successful and failed return value. docx-rwxrwxrwx 1 rootroot 13485 Au  G 14:51 Linux 0.12 Task Scheduler guess. docx-rwxrwxrwx 1 rootroot 11934 13:20 linux BIOS boot. DOCX-RWXRWXRWX 1 rootroot 13349 19:38 the use of the DD command in Linux. docx-rwxrwxrwx 1 rootroot 13802 in 19:20 Linux use the DD command to modify the AS86 assembly after compiling the linked program. DOCX-RWXRWXRWX 1 Rootroo T 15118 16:47 Linux boot code boot.s understanding. DOCX-RWXRWXRWX 1 Rootroot 16005 the BIOS boot process for the Linux operating system. 14:25 1 RO Otroot 14755 01:29 linux proc file system. DOCX-RWXRWXRWX 1 rootroot 12777 1 23:50 mot file single-line checksum with global checksum. DOCX-RWXRWXRWX 1 root Root 131404 14:50 OffIce Excel implements the drop-down bar. docx-rwxrwxrwx 1 rootroot 86665 11:44 Modify the body default font in Office 2016. DOCX-RWXRWXRWX 1 Rootroot 113476 9 21:07 ppt Modify the icon for the inserted object. DOCX-RWXRWXRWX 1 Rootroot 13636 The 22:23 method differences between Python2 Python3 and __bool__. DOCX-RWXRWXRWX 1 Rootro The differences of division function between Python2 and Python3 in OT 13924 1 23:32. docx-rwxrwxrwx 1 rootroot 14551 The difference between 23:09 Python2 and Python3 in different classes of objects. doc X-RWXRWXRWX 1 Rootroot 14101 A few small knowledge points of the Python ABC module. DOCX-RWXRWXRWX 1 rootroot 13581 9 23:24 python oop container design time-fits The introduction of the list method. docx-rwxrwxrwx 1 rootroot 12890 14:29 python isinstance usage. docx-rwxrwxrwx 1 rootroot 13899 9 22:46 Pytho The Extend method of the list in N. docx-rwxrwxrwx 1 rootroot 13142 02:22 The use of __new__ methods in Python. docx-rwxrwxrwx 1 rootroot 13280 20 23 : The properties of the class are implemented using the property in Python. docx-rwxrwxrwx 1 rootroot 13257 22:39 Python custom class comparison method. docx-rwxrwxrwx 1 Rootroot 1389 7 22:45 The properties and information of the most basic class in Python. docx-rwxrwxrwx 1 rootroot 13567 11:53 usage in Python. @classmethod 1 root Root 14859 15:32 PThe Join function usage in Ython. docx-rwxrwxrwx 1 rootroot 13832 01:38 Simple summary of Weakref python. Docx-rwxrwxrwx 1 rootroot 13314 16 20 : A summary of the hashing common sense in Python. docx-rwxrwxrwx 1 rootroot 13822 19:15 a static function usage in Python. docx-rwxrwxrwx 1 rootroot 15223 11 00:1 2 usage of the @ symbol in Python -1.docx-rwxrwxrwx 1 rootroot 162 23:20 ~ $Python and XLWT write Chinese to the Excel file. docx-rwxrwxrwx 1 rootroot 13519 Au  G 20:01 Python implements a function that accepts any number of arguments. DOCX-RWXRWXRWX 1 rootroot 15114 20:31 python to achieve more subtraction loss solution greatest common divisor. DOCX-RWXRWXRWX 1 Rootroot 13425 22:51 The use of notimplementederror in Python programming. DOCX-RWXRWXRWX 1 rootroot 13345 The 22:27 python implements a method that allows invocation only once by property means. DOCX-RWXRWXRWX 1 rootroot 13586 11:15 python basic operations for properties in object-oriented programming. DOCX-RWXRWXRWX 1 rootroot 139264 2 23:50 simulink parse Band Can message information with coefficients and offsets. docx-rwxrwxrwx 1 rootroot 13542 17:22 TAOCP 1.1 Exercise 1 answer and think. docx-rwxrwxrwx 1 rootroot 13683 19 16:0 5 TAOCP The greatest common divisor algorithm is understood. DOCX-RWXRWXRWX 1 rootroot 13539 the 18:41 assembler installed on Ubuntu. AS86 1 docx-rwxrwxrwx rootroot 12 14 : Windows10 Installation Bash.docx-RWXRWXRWX 1 rootroot 258 23:19 xlwt_demo.py-rwxrwxrwx 1 rootroot 13353 18 02:07 Use the __del__ method to ensure that the file is closed when the Python file is processed. DOCX-RWXRWXRWX 1 rootroot 127142 7 21:07 use Polyspace for model detection. DOCX-RWXRWXRWX 1 rootroot 12430 21 23:22 using Python and XLWT Writes Chinese to an Excel file. docx-rwxrwxrwx 1 rootroot 13303 19 19:06 The method of dividing the divide to find greatest common divisor. docx

Among them, xlwt_demo.py is to write the Python script in advance, the code is as follows:

#!/usr/bin/python#-*-coding-cp936-*-Import osimport xlwtfrom xlwt importworkbook book = Workbook () Sheet1 =book.add_she ET (' list ') Row_num = 0for f inos.listdir ('. '):  sheet1.write (row_num,0,f)  row_num + = 1book.save (' Demo.xls ')

Program Run Result:

GREY@DESKTOP-3T80NPQ:/MNT/E/01_WORKSPACE/01_DOCS/02_BLOG/2017 year/August $ python xlwt_demo.pytraceback (mostrecent call Last): File "xlwt_demo.py", Line, 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) 

As you can see from the tips above, this tool supports ASCII code by default. Even though I specified the encoded format at the beginning of the code, I still had a problem. If so, then there must be a special use of XLWT. After reviewing the document, modify the code as follows:

#!/usr/bin/python#-*-coding:cp936-*-import osimport xlwtfrom xlwt importworkbook book =workbook (encoding= ' utf-8 ') Sheet1 =book.add_sheet (' list ') Row_num = 0for f inos.listdir ('. '):  sheet1.write (row_num,0,f)  row_num + = 1book.save (' Demo.xls ')

The results of the program execution are as follows:

GREY@DESKTOP-3T80NPQ:/MNT/E/01_WORKSPACE/01_DOCS/02_BLOG/2017 year/August $ python xlwt_demo.py

GREY@DESKTOP-3T80NPQ:/MNT/E/01_WORKSPACE/01_DOCS/02_BLOG/2017 year/August $ ls

Compilation and linking of As86 assembler. The properties of the class are implemented in Docx Python using the property. docx

The use of access functions in the C language. A comparison operation method for custom classes in docx python. docx

Merge strings in the C language. The properties and information of the most basic classes in docx python. docx

Dynamic memory allocation in the C language. @classmethod usage in docx python. docx

Demo.xls the Join function usage in Python. docx

The GNU glibc improves dynamic memory management in the C language standard library. Weakref Simple summary of docx python. docx

The return value of the function execution succeeded and failed in the GNU glibc library. Hash Common sense Summary in docx python. docx

Linux 0.12 task scheduling guessing. A static function usage in docx python. docx

Linux BIOS boot. Use of the @ symbol in docx python -1.docx

The use of the DD command in Linux. docx ~ $Python and XLWT write Chinese. docx to an Excel file

Linux uses the DD command to modify the AS86 assembly after compiling the linked program. Docx Python implements a function that accepts any number of arguments. docx

Linux boot code boot.s understanding. docx python For more subtractive loss solving greatest common divisor. docx

BIOS boot process for Linux operating system. Use of Notimplementederror in docx python programming. docx

Linux proc file system. Docx Python implements a method that allows invocation only once by property means. docx

The single-line checksum of the MOT file is a global check. docx python basic operation of attributes in object-oriented programming. docx

Office Excel implements the drop-down bar. docx Simulink parses the can message information with coefficients and offsets. docx

The body default font is modified in Office 2016. docx TAOCP in 1.1 Exercise 1 answer and think. docx

The icon for the inserted object is modified in PPT. Greatest common divisor algorithm in docx TAOCP. docx

Differences in __bool__ methods in Python2 and Python3. docx Ubuntu installs as86 assembler. docx

The difference of division function between Python2 and Python3. docx WINDOWS10 Installation Bash.docx

The difference between Python2 and Python3 in different classes of objects. docx xlwt_demo.py

Several small knowledge points of the Python ABC module. DOCX uses the __del__ method to ensure that files are closed when the Python file is processed. docx

The Python OOP container is designed with a reasonable introduction to the list method. docx uses Polyspace for model detection. docx

Isinstance usage in Python. docx writes Chinese. docx to an Excel file using Python and XLWT

The Extend method for list in Python. docx greatest common divisor. docx

The use of the __new__ method in Python. docx

The Excel file has been generated with the following details:

Chinese characters written successfully.

It is worth noting that the encoding cp936 under Windows can be written successfully, but only in Linux can be used utf-8. This is a very interesting phenomenon, I have not yet been able to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.