如何在Python中使用xlsxwriter将德语写入电子表格

2024-05-13 07:18:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经试过了,但是当我打开Excel电子表格时,整个Excel文件是空白的。还有别的办法吗?在

import xlsxwriter

....
    sheet.write(1, 27, "Französisch".decode('latin1'), bold)

Tags: 文件importexcel空白writesheet电子表格decode
2条回答

Excel和XlsxWriter使用ASCII或UTF-8。要在Python2中编写这样的字符串:

  1. 将文件编码为UTF-8。在
  2. 在文件的开头包含“coding”指令。在
  3. 使用u“”表示Unicode字符串。在

像这样:

# _*_ coding: utf-8

import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write('B3', u'Französisch')

workbook.close()

enter image description here

在python3中,您只需要将文件编码为UTF-8。在

参见Unicode examples in XlsxWriter docs。在

我看过你的问题做了一些调查,找到了一个例子。在

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hallo')

# Text with formatting.
worksheet.write('A2', 'Welt', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

# Insert an image.
worksheet.insert_image('B5', 'logo.png')

workbook.close()

应该在工作;) 更多信息请访问:XlsxWriter

相关问题 更多 >