I'm using xlsxwriter to create an Excel file. Currently, I'm doing something like this:
import datetime
import pandas as pd
import xlsxwriter
workbook = xlsxwriter.Workbook('Test.xlsx')
worksheet = workbook.add_worksheet()
datetime_format = workbook.add_format({
'font_size': 12
})
worksheet.write('C2', datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), datetime_format)
# Rest removed for brevity. I simply have many stuff that
# I manually write to make the design as I want.
workbook.close()
writer = pd.ExcelWriter('Test.xlsx', engine='xlsxwriter')
result.to_excel(writer, startrow=6, startcol=3) # Start from D7.
writer.save()
Now, this seems to rewrite everything that I previously wrote manually using xlsxwriter. Also, I noticed that xlsxwriter does not have support by default to write data frames, am I right? How can I combine xlsxwriter and pandas to manually write some stuff to do Excel sheet, and then later to write all the data frame stuff starting from the specific cell?
You need to shift things around. I would also recommend to read the XlsWritter documentation on how to use it with pandas.
writer = pd.ExcelWriter('Test.xlsx', engine='xlsxwriter')
workbook = writer.book
result.to_excel(writer, sheet_name='Sheet1', index=False, startrow=6, startcol=3) # Start from D7.
worksheet = writer.sheets['Sheet1']
datetime_format = workbook.add_format({
'font_size': 12
})
worksheet.write('C2', datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), datetime_format)
writer.save()
First, we create our writer object using pd.ExcelWriter() and passing xlswriter as the engine. Once we have our writer we can access our workbook using the .book class vriable. At this time, we can write our data into our workbook using df.to_excel(). We will use the sheet_name argument of to_excel to create and name our worksheet. At this point we can acess our worksheet using the .sheets class variable and passing our sheetname as a key - in our case Sheet1.
From there you can write additional values to your sheet with or without using a pandas object - as shown in the code below.
Related
I create a excel file from a dataframe:
#writer = pd.ExcelWriter('test_1.xlsx', engine='xlsxwriter')
#uniq_pros.to_excel(writer, sheet_name='Sheet1')
#writer.save()
how can make it protect from editing this excell file?
i want be only for view from users...
You can do that as follows using the XlsxWriter worksheet protect() method:
import pandas as pd
# Create a Pandas dataframe from some 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('test_1.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']
# Set protection for the worksheet.
worksheet.protect()
# Close the Pandas Excel writer and output the Excel file.
writer.save()
Output:
You can also add a password if required.
"Click on the “Review” tab on the main Excel ribbon. Click “Protect Sheet.” Enter the password you would like to use to unlock the sheet in the future. Re-enter the password you made to confirm that you remember it and then click “OK." (in Excel itself)
In python you could probably encrypt it:
Here are some helpful sources:
https://blog.aspose.com/2021/06/01/encrypt-and-decrypt-excel-files-in-python/
Password Protecting Excel file using Python
https://www.easyxls.com/manual/tutorials/python/protect-excel-sheet-cells.html
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.
I have a requirement to read an xlsm file and update some of the sheets in the file. I want to use pandas for this purpose.
I tried answers presented in the following post. I couldn't see the VBA macros when I add the VBA project back.
https://stackoverflow.com/posts/28170939/revisions
Here are the steps I tried,
Extracted the VBA_project.bin out of the original.xlsm file and then
writer = pd.ExcelWriter('original.xlsx', engine='xlsxwriter')
workbook = writer.book
workbook.filename = 'test.xlsm'
workbook.add_vba_project('vbaProject.bin')
writer.save()
With this I don't see the VBA macros attached to "test.xlsm". The result is the same even if I write it to the "original.xlsm" file.
How do I preserve the VBA macros or add them back to the original xlsm file?
Also, is there a way I can open the "xlsm" file itself rather than the "xlsx" counterpart using pd.ExcelWriter?
You can do this easily with pandas
import pandas as pd
import xlrd
# YOU MUST PUT sheet_name=None TO READ ALL CSV FILES IN YOUR XLSM FILE
df = pd.read_excel('YourFile.xlsm', sheet_name=None)
# prints all sheets
print(df)
Ah, I see. I still can't tell what you are doing, but here are a few general samples of code to get Python to communicate with Excel.
Read contents of a worksheet in Excel:
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
df = pd.read_excel('C:\\your_path\\test.xls', sheetname='Sheet1')
************************************************************************************
Use Python to run Macros in Excel:
import os
import win32com.client
#Launch Excel and Open Wrkbook
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\your_path\excelsheet.xlsm") #opens workbook in readonly mode.
#Run Macro
xl.Application.Run("excelsheet.xlsm!modulename.macroname")
#Save Document and Quit.
xl.Application.Save()
xl.Application.Quit()
#Cleanup the com reference.
del xl
Write, from Python, to Excel:
import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('C:/your_path/ranges_and_offsets.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some simple text.
worksheet.write('A1', 'Hello')
# Text with formatting.
worksheet.write('A2', 'World', bold)
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
workbook.close()
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
ws.append([1, 2, 3])
# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()
# Save the file
wb.save("C:\\your_path\\sample.xlsx")
How do I edit spreadsheets using pandas, or any other library.
I have a CSV where I do the data reading and some filters, which I intend to save in an XLSX worksheet ready.
But when I try to send the dataframe to this XLSX worksheet, the file is overwritten by removing all existing edits and sheets in the worksheet.
I'm trying to do so.
excel_name = 'data/nessus/My Scans/Janeiro_2019/teste.xlsx'
writer = pd.ExcelWriter(excel_name, engine='xlsxwriter')
df5.to_excel(writer, sheet_name='FullExport', index=False)
workbook=writer.book
worksheet = writer.sheets['FullExport']
writer.save()
I think I'm doing something wrong, but I can not solve it.
PS:
This dataframe should be sent to the sheet called "FullExport" on line 2
In pandas version 0.24 they will be an option for mode='a'; however; right now you will have to:
writer = pd.ExcelWriter(excel_name, engine='openpyxl')
writer.book = load_workbook(excel_name)
df5.to_excel(writer, sheet_name='FullExport', index=False)
writer.save()
write.close() # i think close() already runs the save function above
I am trying to use ExcelWriter to write/add some information into a workbook that contains multiple sheets.
First time when I use the function, I am creating the workbook with some data. In the second call, I would like to add some information into the workbook in different locations into all sheets.
def Out_Excel(file_name,C,col):
writer = pd.ExcelWriter(file_name,engine='xlsxwriter')
for tab in tabs: # tabs here is provided from a different function that I did not write here to keep it simple and clean
df = DataFrame(C) # the data is different for different sheets but I keep it simple in this case
df.to_excel(writer,sheet_name = tab, startcol = 0 + col, startrow = 0)
writer.save()
In the main code I call this function twice with different col to print out my data in different locations.
Out_Excel('test.xlsx',C,0)
Out_Excel('test.xlsx',D,10)
But the problem is that doing so the output is just the second call of the function as if the function overwrites the entire workbook. I guess I need to load the workbook that already exists in this case?
Any help?
Use load_book from openpyxl - see xlsxwriter and openpyxl docs:
import pandas as pd
from openpyxl import load_workbook
book = load_workbook('test.xlsx')
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name='tab_name', other_params)
writer.save()
Pandas version 0.24.0 added the mode keyword, which allows you to append to excel workbooks without jumping through the hoops that we used to have to do. Just use mode='a' to append sheets to an existing workbook.
From the documentation:
with ExcelWriter('path_to_file.xlsx', mode='a') as writer:
df.to_excel(writer, sheet_name='Sheet3')
You could also try using the following method to create your Excel spreadsheet:
import pandas as pd
def generate_excel(csv_file, excel_loc, sheet_):
writer = pd.ExcelWriter(excel_loc)
data = pd.read_csv(csv_file, header=0, index_col=False)
data.to_excel(writer, sheet_name=sheet_, index=False)
writer.save()
return(writer.close())
Give this a try and let me know what you think.