简体   繁体   English

使用python保存.xlsx文件时保持相同的文件格式

[英]keeping the same file format when saving .xlsx file using python

Im working on a project where I have to take excel file make changes to the data and save it 我正在一个项目中工作,我必须将Excel文件作为数据进行更改并保存

from pandas import ExcelWriter
import pandas as pd

dfs = pd.read_excel("infile.xlsx")

#manuplate data 

writer = ExcelWriter('outfile.xlsx')
dfs.to_excel(writer,'Sheet5')
writer.save()

The problem I have is the newly saved excel file does not have the same format(cell widht, bold borders) as the input file. 我的问题是新保存的Excel文件与输入文件的格式不同(单元格宽度,粗体边框)。 What can I do to solve this issue? 我该怎么做才能解决这个问题?

You can't preserve the formatting because pandas throws away all that information upon import. 您无法保留格式,因为熊猫在导入时会丢弃所有这些信息。 You would need to specify the formatting options you want in the output with the ExcelWriter object. 您需要使用ExcelWriter对象在输出中指定所需的格式设置选项。 If you use the option engine='xlsxwriter' you can then use all the xlsxwriter formatting options before writing the final file. 如果使用选项engine='xlsxwriter' ,则可以在写入最终文件之前使用所有xlsxwriter格式化选项。 You can find more details in the XlsxWriter documentation. 您可以在XlsxWriter文档中找到更多详细信息

Example: 例:

import pandas as pd

# This removes the default header style so we can override it later
import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data1': [10, 20, 30, 20, 15, 30, 45],
                   'Data2': [90, 80, 30, 15, 88, 34, 41]})


# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Create Format objects to apply to sheet
# https://xlsxwriter.readthedocs.io/format.html#format-methods-and-format-properties
red_bold = workbook.add_format({'bold': True, 'font_color': 'red'})
border = workbook.add_format({'border':5, 'border_color':'blue'})

#Apply formatting to sheet
worksheet.set_column('C:C', None, red_bold)
worksheet.set_column('A1:A8', None, border)

# Apply a conditional format to a cell range.
worksheet.conditional_format('B2:B8', {'type': '3_color_scale'})

# Close the Pandas Excel writer and output the Excel file.
writer.save()

暂无
暂无

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

相关问题 Python 将 xlsx 文件保存在不同的文件夹中,然后声明 - Python is saving the xlsx file in a different folder then declared 使用python将内容从.txt文件存储到.xlsx时出错 - Error when storing content from a .txt file to a .xlsx using python 如何通过 uniqueid 从 xlsx 文件中提取数据并使用 Python 将该数据写入另一个具有相同列名的 xlsx 文件? - How can I pull data by uniqueid from an xlsx file and write that data to another xlsx file with the same column name using Python? VBA 脚本触发 Python 并保存 xlsx 文件 - VBA script to trigger Python and saving down xlsx file 使用comtypes保存PowerPoint演示文稿时使用文件格式常量 - Using file format constants when saving PowerPoint presentation with comtypes openpyxl,保存/使用后如何关闭/shutdown.xlsx 文件/会话? - openpyxl, how to close/shutdown .xlsx file/session after saving/using it? 在 Python (Pandas/Openpyxl) 中修改的 xlsx 文件与在 Excel 中修改的同一个 xlsx 文件具有不同的属性 - Xlsx file modified in Python (Pandas/Openpyxl) has not same properties as the same xlsx file modified in Excel 使用 python 脚本将 csv 文件 pipe 定界为 xlsx 压缩格式 - Convert csv file pipe delimited to xlsx compressed format using python script 如何使用python 3在xlsx文件中搜索数据? - How to search for data in an xlsx file using python 3? 使用python将tsv文件转换为xls/xlsx - convert a tsv file to xls/xlsx using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM