简体   繁体   English

Python:如何使用xlsxwriter更改单元格num类型

[英]Python: how to change cell num type using xlsxwriter

I am using python to convert my .csv file to xlsx files , file conversion happens successfully. 我正在使用python将我的.csv文件转换为xlsx文件 ,文件转换成功完成。 At the same time I would like to change the cell num type also, like where ever there are float type xx.xxxx, that will be reduced to .xx (2 decimals) values. 同时,我也想更改单元格num类型,就像有浮点类型xx.xxxx的地方一样,它将被减小为.xx(两位十进制)的值。

input test.csv sheet, seperated by "," 输入test.csv表,以“,”分隔

Name        Date        Total request   sas rate
rasta777    16062017    387865          89.3456
rasta777    16062017    291293          67.675
rasta777    16062017    256418          100
rasta777    16062017    251558          98.98

I have already tried the below python script using xlsxwriter , but getting an error. 我已经使用xlsxwriter尝试了以下python脚本,但出现错误。

import os
import glob
import csv
from xlsxwriter.workbook import Workbook

for csvfile in glob.glob(os.path.join('.', 'hourly.csv')):
workbook = Workbook(csvfile + '.xlsx')
worksheet = workbook.add_worksheet()
format = workbook.add_format()
format.set_bold()
format.set_font_color('blue')
format.set_num_format('0x02')
format1 = workbook.add_format({'font_color': 'blue'})
with open(csvfile, 'rb') as f:
    reader = csv.reader(f)
    for r, row in enumerate(reader):
        for c, col in enumerate(row):
            worksheet.write(r, c, col)
            worksheet.set_row(0, 20, format)
            worksheet.set_column('A:P', format)
workbook.close()

getting error that 得到的错误

"TypeError: unsupported operand type(s) for *: 'Format' and 'int'" “ TypeError:*:'格式'和'整数'不支持的操作数类型”

My second problem is generated output (getting output if I remove worksheet.set_column('A:P', format) ) gives cell type as text, whereas my data is all of numerical values except headers. 我的第二个问题是生成输出(如果删除worksheet.set_column('A:P', format) ,则获得输出)将单元格类型设置为文本,而我的数据是除标头外的所有数值。

Last time I checked XlsxWriter's worksheet did not support changing the cell type after the value was written. 上次我检查XlsxWriter的worksheet不支持在写入值后更改单元格类型。 When you want to write a value of specific type, you have to write it using a method specific to a type, like so: 当您要编写特定类型的值时,必须使用特定于该类型的方法来编写它,如下所示:

worksheet.write_datetime(idx, fld_idx, fld, date_format)

You also have to specify format while writing and I don't think it's possible to change the format after the fact. 您还必须在编写时指定格式, 我认为事后不能更改格式。

Generally, I've learned by experience with XlsxWriter that it's better to prepare everything you need before writing and then write the cell once with all the correct values. 通常,我从XlsxWriter的经验中学到,最好在编写之前准备好所需的所有内容,然后使用所有正确的值一次编写单元格。 So eg I use the date_format predefined the usual way while writing the datetime to the cell. 因此,例如,在将datetime时间写入单元格时,我通常使用预定义的date_format That works. 这样可行。

Read up on type-specific methods here: http://xlsxwriter.readthedocs.io/worksheet.html , in particular: 在此处阅读特定于类型的方法: http : //xlsxwriter.readthedocs.io/worksheet.html ,尤其是:

write_string()
write_number()
write_blank()
write_formula()
write_datetime()
write_boolean()
write_url()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用xlsxwriter更改图例字体大小 - python - How to change the legend font size using xlsxwriter - python python xlsxwriter在使用write_row时更改所有单元格宽度 - python xlsxwriter change all cell widths when using write_row 使用Python和xlsxwriter将单元格引用与单元格数量连接起来 - Using Python and xlsxwriter to concatenate cell references with cell count 如何使用xlsxwriter将主题单元格样式添加到单元格? - How can I add Themed Cell Styles to a cell using xlsxwriter? 如何使用python-xlsxwriter从excel文件中的单元格中删除绿色箭头? - How can I remove the green arrow from a cell on excel file using python-xlsxwriter? 使用 python 和 xlsxwriter 以另一种格式将新文本添加到 excel 单元格中 - Adding new text into excel cell with another format using python & xlsxwriter 使用python变量和动态单元格引用的xlsxwriter公式 - xlsxwriter Formula using python variables and dynamic cell referencing 如何强制 Python XlsxWriter 以自定义格式写入单元格 - How to force Python XlsxWriter to write to cell in custom format 如何使用python xlsxwriter有条件地格式化单元格值 - how to use python xlsxwriter to conditionally format on cell value 如何使用Python / XlsxWriter将单个单元格设置为数字和粗体格式 - How to set a single cell to both number and bold format with Python/XlsxWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM