读取CSV文件,并写入文本自动换行优秀文本换行 [英] Reading csv file and writing the df to excel with text wrap

查看:1931
本文介绍了读取CSV文件,并写入文本自动换行优秀文本换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到以下输出。所有的行和列都是除了标题之外的文本包装:



 导入pandas作为pd 
导入pandas.io.formats.style
从pandas导入os
导入ExcelWriter
导入numpy作为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 \\\\\\\\\\\\\\\\\\\\\\\\\\'')
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()

格式适用于除标题以外的所有行和列。我不知道为什么格式不适用于第一列(标题),或者我想手动添加列标题编号,如0,1,2等,所以我会打开标题,因此所有的行和列将被格式化

在上面的截图中,换行文本不适用于A1到E1,C1列有很多空间的标题。如果我手动点击包装文本,它将被对齐,否则所有的头文件都不会使用文本包装来格式化。 解决方案

几个问题:
$ b


  1. 您的代码正确尝试格式化标题,但是当您使用 .to_excel ()你告诉它从行/ col 1,1 开始。尽管这些单元格是从 0,0 编号的。所以,如果你改变:

    $ $ $ $ $ $ c $ df.to_excel(writer,sheet_name ='Sheet1',startrow = 0,startcol = 0,你将会看到col A 和行 1 都是格式化的:



    Col A 0 第1行是<$ c当使用Pandas编写头文件时,它会应用自己的格式,它会覆盖您提供的格式。为了解决这个问题,关掉头文件并且只写第一行的数据并手工写头。



     将pandas导入为pd 
    导入pandas.io.formats。样式
    从pandas导入os
    ExcelWriter
    导入numpy作为np
    $ b从xlsxwriter.utility导入xl_rowcol_to_cell

    编写者= pd.ExcelWriter ('test1.xlsx',engine ='xlsxwriter',options = {'strings_to_numbers':True},date_format ='mmmm dd yyyy')
    #df = pd.read_csv(D:\\Users (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.s et_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)

    #手动编写头文件
    for colx,枚举值(df.columns.values):
    worksheet.write(0,colx,value)

    writer.save()

    哪个会给你:




    $ b 注意:也可以告诉Pandas使用的样式,或强制它到 None ,所以它会继承你自己的风格。这种方法唯一的缺点是所需的方法取决于正在使用的Pandas版本。这种方法适用于所有版本。


    I am trying to get the following output. All rows and columns are text wrapped except the header though:

    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. 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

    In the above screenshot wrap text is not applied to A1 to E1, C1 column has header with lot of space. 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. The cells though are numbered from 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:

      i.e. Col A is 0 and Row 1 is 0

    2. When using Pandas to write the header, it applies its own format which will overwrite the formatting you have provided. To get around this, turn off headers and get it to only write the data from row 1 onwards and write the header manually.

    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. The only drawback with that approach is that the method required to do that depends on the version of Pandas that is being used. This approach works for all versions.

    这篇关于读取CSV文件,并写入文本自动换行优秀文本换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆