如何使用 python 设置 XLSX 文件中单元格的数字格式
how to set numeric format of a cell in XLSX file using python
我想使用 python 脚本为 XLSX 文件中的列或单元格设置数字格式。
转换脚本获取 CSV 文件并将其转换为 XLSX。我特意把header当成普通行,因为final script在最后会根据指定的命令行参数以各种方式进行转换。
下面的示例仅显示我尝试将数字格式设置为列或单元格。
我做错了什么?
使用这段代码,我设法将对齐方式设置为右对齐。但是任何设置数字格式的方法都失败了。 XLSX 文件仍然保留单元格左上角的绿色三角形,并拒绝将其视为数字单元格。
随附的屏幕截图显示 "wrong" 结果。
----数据文件----
a,b,c,d,e
q,1,123,0.4,1
w,2,897346,.786876,-1.1
e,3,9872346,7896876.098098,2.098
r,4,65,.3,1322
t,5,1,0.897897978,-786
----python脚本----
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import pandas
import xlsxwriter
def is_type( value ):
'''Function to identify true type of the value passed
Input parameters: value - some value which type need to be identified
Returned values: Type of the value
'''
try:
int(value)
return "int"
except:
try:
float(value)
return "float"
except:
return "str"
csv_file_name = "test37.csv"
xls_file_name = "test37.xlsx"
# Read CSV file to DataFrame
df = pandas.read_csv(csv_file_name, header=None, low_memory=False, quotechar='"', encoding="ISO-8859-1")
# Output DataFrame to Excel file
df.to_excel(xls_file_name, header=None, index=False, encoding="utf-8")
# Create writer object for output of XLSX file
writer = pandas.ExcelWriter(xls_file_name, engine="xlsxwriter")
# Write our Data Frame object to newly created file
xls_sheet_name = os.path.basename(xls_file_name).split(".")[0]
df.to_excel(writer, header=None, index=False, sheet_name=xls_sheet_name, float_format="%0.2f")
# get objects for workbook and worksheet
wb = writer.book
ws = writer.sheets[xls_sheet_name]
ws.set_zoom(120)
num_format1 = wb.add_format({
'align': 'right'
})
num_format2 = wb.add_format({
'align': 'right',
'num_format': '0.00'
})
num_format3 = wb.add_format()
num_format3.set_num_format('0.00')
ws.set_column('D:D', None, num_format1)
ws.set_column('D:D', None, num_format2)
for column in df.columns:
for row in range(1,len(df[column])):
if is_type(df[column][row]) == "int":
#print("int "+str(df.iloc[row][column]))
ws.write( row, column, df.iloc[row][column], num_format2 )
elif is_type(df[column][row]) == "float":
#print("float "+str(df.iloc[row][column]))
ws.write( row, column, df.iloc[row][column], num_format2 )
else:
pass
wb.close()
writer.save()
exit(0)
问题与您的 xlsxwriter 脚本无关,而在于您在 Pandas 中导入 csv 的方式。您的 csv-file 有一个 header,但您在 pd.read_csv() 中指定没有 header。因此,Pandas 也将 header 行解析为数据。因为 header 是一个字符串,所以整个列被导入为一个字符串(而不是整数或浮点数)。
只需删除 pd.read_csv 和 df.to_excel() 中的 'header=None',它应该可以正常工作。
所以:
...<first part of your code>
# Read CSV file to DataFrame
df = pandas.read_csv(csv_file_name, low_memory=False, quotechar='"', encoding="ISO-8859-1")
# Output DataFrame to Excel file
df.to_excel(xls_file_name, index=False, encoding="utf-8")
<rest of your code>...
我想使用 python 脚本为 XLSX 文件中的列或单元格设置数字格式。
转换脚本获取 CSV 文件并将其转换为 XLSX。我特意把header当成普通行,因为final script在最后会根据指定的命令行参数以各种方式进行转换。
下面的示例仅显示我尝试将数字格式设置为列或单元格。
我做错了什么?
使用这段代码,我设法将对齐方式设置为右对齐。但是任何设置数字格式的方法都失败了。 XLSX 文件仍然保留单元格左上角的绿色三角形,并拒绝将其视为数字单元格。 随附的屏幕截图显示 "wrong" 结果。
----数据文件----
a,b,c,d,e
q,1,123,0.4,1
w,2,897346,.786876,-1.1
e,3,9872346,7896876.098098,2.098
r,4,65,.3,1322
t,5,1,0.897897978,-786
----python脚本----
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import pandas
import xlsxwriter
def is_type( value ):
'''Function to identify true type of the value passed
Input parameters: value - some value which type need to be identified
Returned values: Type of the value
'''
try:
int(value)
return "int"
except:
try:
float(value)
return "float"
except:
return "str"
csv_file_name = "test37.csv"
xls_file_name = "test37.xlsx"
# Read CSV file to DataFrame
df = pandas.read_csv(csv_file_name, header=None, low_memory=False, quotechar='"', encoding="ISO-8859-1")
# Output DataFrame to Excel file
df.to_excel(xls_file_name, header=None, index=False, encoding="utf-8")
# Create writer object for output of XLSX file
writer = pandas.ExcelWriter(xls_file_name, engine="xlsxwriter")
# Write our Data Frame object to newly created file
xls_sheet_name = os.path.basename(xls_file_name).split(".")[0]
df.to_excel(writer, header=None, index=False, sheet_name=xls_sheet_name, float_format="%0.2f")
# get objects for workbook and worksheet
wb = writer.book
ws = writer.sheets[xls_sheet_name]
ws.set_zoom(120)
num_format1 = wb.add_format({
'align': 'right'
})
num_format2 = wb.add_format({
'align': 'right',
'num_format': '0.00'
})
num_format3 = wb.add_format()
num_format3.set_num_format('0.00')
ws.set_column('D:D', None, num_format1)
ws.set_column('D:D', None, num_format2)
for column in df.columns:
for row in range(1,len(df[column])):
if is_type(df[column][row]) == "int":
#print("int "+str(df.iloc[row][column]))
ws.write( row, column, df.iloc[row][column], num_format2 )
elif is_type(df[column][row]) == "float":
#print("float "+str(df.iloc[row][column]))
ws.write( row, column, df.iloc[row][column], num_format2 )
else:
pass
wb.close()
writer.save()
exit(0)
问题与您的 xlsxwriter 脚本无关,而在于您在 Pandas 中导入 csv 的方式。您的 csv-file 有一个 header,但您在 pd.read_csv() 中指定没有 header。因此,Pandas 也将 header 行解析为数据。因为 header 是一个字符串,所以整个列被导入为一个字符串(而不是整数或浮点数)。
只需删除 pd.read_csv 和 df.to_excel() 中的 'header=None',它应该可以正常工作。
所以:
...<first part of your code>
# Read CSV file to DataFrame
df = pandas.read_csv(csv_file_name, low_memory=False, quotechar='"', encoding="ISO-8859-1")
# Output DataFrame to Excel file
df.to_excel(xls_file_name, index=False, encoding="utf-8")
<rest of your code>...