我已经为xlsxwriter网站提供了这个基本代码。 据他们说,这段代码应该打开一个带有hello写在A1方块中的Excel文件,但似乎没有创建任何东西,并且在扫描我的计算机找到该文件后,我的怀疑得到了证实。
import xlsxwriter workbook = xlsxwriter.Workbook("hello.xlsx") worksheet = workbook.add_worksheet() worksheet.write('A1', 'Hello world') workbook.close()I have taken this basic code for the xlsxwriter website. According to them, this code should open a Excel file with hello written in the A1 square, but nothing seems to have been created and after having scanned my computer for the file my suspicion was confirmed.
import xlsxwriter workbook = xlsxwriter.Workbook("hello.xlsx") worksheet = workbook.add_worksheet() worksheet.write('A1', 'Hello world') workbook.close()最满意答案
这段代码确实有效:
import xlsxwriter workbook = xlsxwriter.Workbook(r'C:\hello.xlsx') worksheet = workbook.add_worksheet() worksheet.write('A1', 'Hello world') workbook.close()您应该指定要创建的文件的位置。 例如r'C:\hello.xlsx'而不仅仅是文件的名称。 此外,您需要关闭工作簿,而不是工作表。
仅供参考:要格式化代码,只需将代码缩进四个空格即可。
This code does work:
import xlsxwriter workbook = xlsxwriter.Workbook(r'C:\hello.xlsx') worksheet = workbook.add_worksheet() worksheet.write('A1', 'Hello world') workbook.close()You should specify where the file to be created will go. For example r'C:\hello.xlsx' rather than just the name of the file. Also, you need to close the workbook, not the worksheet.
FYI: To format your code, just indent code by four spaces.
更多推荐
发布评论