问题描述
这是我的 Python 脚本使用 XLSXwriter 遵循的基本流程:
This is the basic flow that my Python script is following with XLSXwriter:
- 创建工作簿
- 创建工作表
- 定义格式
- 在 row1 中写入列标题(利用 Step3 中的格式)
- 在后续行中写入实际数据(无特殊格式)
我可以创建工作簿/工作表.然后,我定义格式并使用COLUMN HEADERS"填充工作表 row1 列 A:H,这表示将写入后续行中每一列的数据类型.这是一次成功.
I can create the workbook/worksheet. I then define formatting and populate worksheet row1 columns A:H with 'COLUMN HEADERS' which signify the type of DATA that will be written into each column on subsequent rows. This much is a success.
我的代码和问题描述如下:
My code and problem description are below:
# write column header information
if total_xls_records == 1:
worksheet1.set_row(0, 15, header_format)
worksheet1.set_column('A:H',30)
worksheet1.write('A1', 'Hostname')
worksheet1.write('B1', 'Serial')
worksheet1.write('C1', 'Manufacturer')
worksheet1.write('D1', 'Model')
worksheet1.write('E1', 'RAM')
worksheet1.write('F1', 'Flash')
worksheet1.write('G1', 'Version')
worksheet1.write('H1', 'SW_Image')
# write device data to spreadsheet
else:
worksheet1.set_row(total_xls_records, 15)
worksheet1.set_column('A:H',30)
worksheet1.write('A2', data[0])
worksheet1.write('B2', data[1])
worksheet1.write('C2', data[2])
worksheet1.write('D2', data[3])
worksheet1.write('E2', data[4])
worksheet1.write('F2', data[5])
worksheet1.write('G2', data[6])
worksheet1.write('H2', data[7])
我正在从列表中提取数据以填充我的单元格.这个 if/else 的结果是:
I'm pulling data in from a list to populate my cells. The result of this if/else is:
- 当我匹配if"语句时,列标题会正确写入电子表格
如果我打开电子表格,我会看到 row1 列 A:H 正确填充并按 header_format 格式化,列宽为 30.
If I open the spreadsheet, I see row1 columns A:H properly populated and formatted per header_format with a column width of 30.
- 当我随后匹配else"语句时,电子表格将被覆盖
如果我打开电子表格,我会看到 row1 完全为空(所有列标题/格式现在都消失了)并且 row2 正确填充了设备数据(无格式).
If I open the spreadsheet, I see row1 is completely empty (all column headers/formatting are now gone) and row2 is properly populated with device DATA (no formatting).
显然,所需的行为是让 row1 带有 COLUMN HEADERS,而 row2-whatever 带有 DATA.我想将数据附加"到行 2 中的电子表格 - 无论行 1 已经写入之后.
Obviously the desired behavior is to have row1 with COLUMN HEADERS and rows2-whatever with DATA. I want to 'append' DATA to the spreadsheet in rows2-whatever AFTER row1 has already been written.
是的,我知道 else 语句当前调用单元格的静态列表"(A2-H2),这意味着 else 的多个匹配项当前会将 DATA 写入同一组单元格.它目前是这样写的,因为我正处于我刚刚描述的问题的故障排除模式".对我来说,在重写我的单元格人口"代码之前解决电子表格覆盖问题是有意义的.
Yes, I'm aware that the else statement currently calls a 'static list' of cells (A2-H2) and that this means multiple matches of else will currently write DATA into the same set of cells. It's currently written this way because I'm in 'troubleshooting mode' for the problem I just described. For me it makes sense to fix the spreadsheet overwrite problem before re-writing my "cell population" code.
预先感谢您提供的任何帮助.
Thanks in advance for any help provided.
推荐答案
XlsxWriter 无法重写文件或重复保存到文件.
XlsxWriter cannot rewrite a file or repeatedly save to a file.
一旦 workbook.close()
被调用,文件就会被写入并关闭.
Once workbook.close()
is called the file is written and closed.
这篇关于带有 XLSXWRITER 的 Python:电子表格被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!