Writing text wrapped Excel Files using Python - python

I am new to Python and I was practicing by processing some CSV files and making an excel file from them. So far I can get the excel file however, I am unable to wrap the cells via python. I have tried multiple ways but none of it would work. Perhaps it is because of my poor understanding of Python. Can anyone suggest me how can I wrap text while writing the excel file? And please explain the code along the way? The error that i am getting for the following code is:
'str' object has no attribute 'alignment'
This is what I have done so far:
df=pd.DataFrame(list(zip(Dticketnumberlist,Dcategorylist)),
columns=['Ticket', 'Category'])
writer = pd.ExcelWriter('Trial Version.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook=writer.book
worksheet = writer.sheets['Sheet1']
wrap_alignment = Alignment(wrap_text=True)
cell.alignment = wrap_alignment

You can use pandas with the xlsxwriter engine (the default).
You need to create a format object by calling the workbook.add_format() method as outlined in the xlsxwriter docs (link here).
Once you've used pandas.DataFrame.to_excel(), you can add the format using worksheet.set_column(). An example of this can be found in the xlsxwriter docs (link here).
I've provided a fully reproducible example below with the expected output.
import pandas as pd
df = pd.DataFrame({'Ticket': ['a','b','c','d'],
'Category': [2,1,4,3]})
writer = pd.ExcelWriter('Trial Version.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook=writer.book
worksheet = writer.sheets['Sheet1']
format = workbook.add_format({'text_wrap': True})
# Setting the format but not setting the column width.
worksheet.set_column('A:B', None, format)
writer.save()
Expected Output:

Use python xlsxwriter
The question answered previously can help you better.

Related

Trouble wrting to Excel

I' am new to Python and trying to write into a merged cell within Excel. I can see the data that is already stored within this cell/row, so I know its there. However when I try to overwrite it nothing happens.
I have tried messing with the index and header as well but nothing seems to work.
import pandas as pd
from openpyxl import load_workbook
Read the excel file into a pandas DataFrame
df = pd.read_excel(file here', sheet_name='Sheet1')
print(df.iloc[8, 2])
Make the changes to the DataFrame
df.iloc[8, 2] = "Bob Smith"
Load the workbook
book = load_workbook(file here)
writer = pd.ExcelWriter(file here, engine='openpyxl')
writer.book = book
Write the DataFrame to the first sheet
df.to_excel(writer, index=False)
Save the changes to the Excel file
writer.save()
import pandas as pd
from openpyxl import *
file="C:/Users/OneDrive/Bureau/draftExcel.xlsx"
df = pd.read_excel(file,sheet_name='sheet1')
df.iat[5,0]='cell is updated'
print(df) # to check first in the terminal if the content of the cell is updated
book=load_workbook(file)
writer=pd.ExcelWriter(file, engine='openpyxl')
df.to_excel(writer,sheet_name='sheet1',index=False)
writer.close()
I tried to make an example from what you explained because you didn't show your code, so I hope it was helpful.
Instead of using .iloc I used .iat so you can update the data in a specific cell in your DataFrame using column_index instead of column_label.
Remember that the Excel file you are working on must be closed while you are editing data with python, if it is open you will get an error.

Appending dataframe to the very same worksheet

I have been searching between different questions and answers regarding open openpyxl and xlsxwriter.
I have a simple question and I am sure there is a simple answer for it.
I have a dataframe which I am trying to append to an existing xlsx file. My issue is that it keeps creating a new worksheet everytime I run the code.
I have tried several solutions but none of them work. here is the code:
output_dict = {'Name': pd.Series(new_name),
'Link': pd.Series(new_url)
}
df = pd.DataFrame(output_dict)
with ExcelWriter('Links.xlsx', engine="openpyxl", mode='a') as writer:
df.to_excel(writer, sheet_name='in')
My question is how to append df to the very same worksheet called 'in'?
Try replacing your with ExcelWriter block with:
with pd.ExcelWriter('Links.xlsx',mode='a') as writer:
df.to_excel(writer, sheet_name='in')
Note the pd.ExcelWriter, not just ExcelWriter.
This is from the pandas documentation here. Second to last code block on the page.

Using XlsxWriter to save CSVs in different sheets of an Excel Workbook

So, my question may sound silly because this is the first time I'm using XlsxWriter. I straight up copied their code from their site but it didn't work.
The code is this:
import pandas as pd
# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
workbook = xlsxwriter.Workbook('filename.xlsx')
worksheet = workbook.add_worksheet()
Even before I could start dealing with CSV files, this basic first run failed, and the following error appeared:
NameError: name 'xlsxwriter' is not defined
I tried using pip install openpyxl as someone said in this previous thread, but it didn't work either. Can someone give me a hand here please?
If you are going to use xlsxwriter directly, outside of Pandas, like you are doing in the last 2 lines of the code above, you will need to import the module in order to use it:
import xlsxwriter
# ... Rest of your code as above.

Exporting a pandas df into an excel file that has formatting already in it?

I've spent hours researching this issue but cant seem to find an answer. I have a template in Excel that has conditional formatting already applied to it. I want to import a pandas df into this already formatted excel file so that the data is being formatted accordingly (color, number format, etc.). Does anyone if this is doable? And if so, how?
Ive considered writing a macro and just importing it into python and applying to the df. Just want to see if there's an easier way that I haven't thought of/found. Thanks!
I would advise to try openpyxl
from openpyxl import load_workbook
book = load_workbook(excelpath) # load excel with formats
writer = pandas.ExcelWriter(excelpath, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, "Sheet1", columns=['a', 'b'], index=False) # only columns 'a' and 'b' will be populated
writer.save()

How to write to an Excel sheet without exporting a dataframe first?

I am trying to write some text to a specific sheet in an Excel file. I export a number of pandas dataframes to the other tabs, but in this one I need only some text - basically some comments explaining how the other tabs were calculated.
I have tried this but it doesn't work:
import pandas as pd
writer=pd.ExcelWriter('myfile.xlsx')
writer.sheets['mytab'].write(1,1,'This is a test')
writer.close()
I have tried adding writer.book.add_worksheet('mytab') and
ws=writer.sheets['mytab']
ws.write(1,1,'This is a test')
but in all cases I am getting: keyerror:'mytab'.
The only solution I have found is to write an empty dataframe to the tab before writing my text to the same tab:
emptydf=pd.DataFrame()
emptydf['x']=[None]
emptydf.to_excel(writer,'mytab',header=False, index=False)
I could of course create a workbook instance, as in the example on the documentation of xlsxwriter: http://xlsxwriter.readthedocs.io/worksheet.html
However, my problem is that I already have a pd.ExcelWriter instance, which is used in the rest of my code to create the other excel sheets.
I even tried passing a workbook instance to to_excel(), but it doesn't work:
workbook = xlsxwriter.Workbook('filename.xlsx')
emptydf.to_excel(workbook,'mytab',header=False, index=False)
Is there any alternative to my solution of exporting an empty dataframe - which seems as unpythonic as it can get?
You mentioned that you used add_worksheet() method from the writer.book object, but it seems to work and do what you wanted it to do. Below I've put in a reproducible example that worked successfully.
import pandas as pd
print(pd.__version__)
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
ws = workbook.add_worksheet('mytab')
ws.write(1,1,'This is a test')
writer.close()
Thought I'd also mention that I'm using pandas 0.18.1.

Categories