简体   繁体   English

读取csv文件并使用文本换行将df写入excel

[英]Reading csv file and writing the df to excel with text wrap

I am trying to get the following output. 我想获得以下输出。 All rows and columns are text wrapped except the header though: 除了标题之外,所有行和列都是文本换行:

Excel截图

import pandas as pd
    import pandas.io.formats.style
    import os
    from pandas import ExcelWriter
    import numpy as np

    from xlsxwriter.utility import xl_rowcol_to_cell
    writer = pd.ExcelWriter('test1.xlsx',engine='xlsxwriter',options={'strings_to_numbers': True},date_format='mmmm dd yyyy')  
    df = pd.read_csv("D:\\Users\\u700216\\Desktop\\Reports\\CD_Counts.csv")
    df.to_excel(writer,sheet_name='Sheet1',startrow=1 , startcol=1, header=True, index=False, encoding='utf8')  
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']

    format = workbook.add_format()
    format1 = workbook.add_format({'bold': True, 'align' : 'left'})
    format.set_align('Center')
    format1.set_align('Center')
    format.set_text_wrap()
    format1.set_text_wrap()
    worksheet.set_row(0, 20, format1)
    worksheet.set_column('A:Z', 30, format)
    writer.save()

format is applied for all rows and columns except header. format适用于除标题之外的所有行和列。 i dont know why format is not applied to first column (Header) or i would like to manually add column header numbers such as 0,1,2 etc so that i will turn of the header therefore all the rows and columns will be formatted 我不知道为什么格式不应用于第一列(标题)或我想手动添加列标题数字,如0,1,2等,以便我将转为标题因此所有的行和列将被格式化

In the above screenshot wrap text is not applied to A1 to E1, C1 column has header with lot of space. 在上面的截图中,换行文本不适用于A1到E1,C1列有很多空格的标题。 if i manually click wrap text it gets aligned else all the header is not formatted using text wrap. 如果我手动点击换行文本它会对齐,否则所有标题都不会使用文本换行进行格式化。

A couple of problems: 一些问题:

  1. Your code is correctly attempting to format the header, but when you create your file using .to_excel() you are telling it to start at row/col 1, 1 . 您的代码正在尝试格式化标题,但是当您使用.to_excel()创建文件时,您会告诉它从第1, 1行开始。 The cells though are numbered from 0, 0 . 虽然细胞编号为0, 0 So if you change to: 所以,如果你改为:

     df.to_excel(writer,sheet_name='Sheet1', startrow=0, startcol=0, header=True, index=False, encoding='utf8') 

    You will see col A and row 1 are both formatted: 您将看到col A和第1行都被格式化:

    excel截图

    ie Col A is 0 and Row 1 is 0 Col A0Row 1 0

  2. When using Pandas to write the header, it applies its own format which will overwrite the formatting you have provided. 使用Pandas编写标题时,它会应用自己的格式,这将覆盖您提供的格式。 To get around this, turn off headers and get it to only write the data from row 1 onwards and write the header manually. 要解决此问题,请关闭标头并使其仅从第1行开始写入数据并手动编写标头。

The following might be a bit clearer: 以下可能更清楚一点:

import pandas as pd
import pandas.io.formats.style
import os
from pandas import ExcelWriter
import numpy as np

from xlsxwriter.utility import xl_rowcol_to_cell

writer = pd.ExcelWriter('test1.xlsx', engine='xlsxwriter', options={'strings_to_numbers': True}, date_format='mmmm dd yyyy')  
#df = pd.read_csv("D:\\Users\\u700216\\Desktop\\Reports\\CD_Counts.csv")
df = pd.read_csv("CD_Counts.csv")
df.to_excel(writer, sheet_name='Sheet1', startrow=1 , startcol=0, header=False, index=False, encoding='utf8')  
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

format_header = workbook.add_format()
format_header.set_align('center')
format_header.set_bold()
format_header.set_text_wrap()
format_header.set_border()

format_data = workbook.add_format()
format_data.set_align('center')
format_data.set_text_wrap()

worksheet.set_column('A:Z', 20, format_data)
worksheet.set_row(0, 40, format_header)

# Write the header manually
for colx, value in enumerate(df.columns.values):
    worksheet.write(0, colx, value)

writer.save()

Which would give you: 哪个会给你:

从熊猫文本换行的标题

Note: It is also possible to tell Pandas the style to use, or to force it to None so it will inherit your own style. 注意:也可以告诉Pandas使用的样式,或强制它为None这样它将继承自己的样式。 The only drawback with that approach is that the method required to do that depends on the version of Pandas that is being used. 该方法的唯一缺点是,执行此操作所需的方法取决于正在使用的Pandas的版本。 This approach works for all versions. 此方法适用于所有版本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM