Add sheet to created workbook from another workbook - python

I create new workbooks via xlsxwriter. In every of them I need to have formated header sheet, which is stored in another template workbook. I know it is impossible to do with xlsxwriter, coz I cannot open template workbook with this.
I thought to do that by xlrd, copy this sheet and then with xlsxwriter write it to created workbook.
But is it possible? To use combination of those two libraries?
I know this question is without even any code, but I'm lame with python and if you could give me any advice or something to deal with my problem I will be gratefull.

xlrd and xlswriter aren't really designed to work together. Consider switching to the pyopenxl library, which allows both reading and writing of spreadsheets and might allow you to do what you need quite easily.

Related

How to persist shapes in python

I have an excel form with shapes. If I change only the sheet contents to Python without changing this form and save it, all the shapes will disappear.
I'm using openpyxl.
How to persist excel shapes in python?
I think you're asking how to encode an Excel file's shapes in openpyxl data, and your goal is to have that data persist when you change the openpyxl data back into an Excel file format.
openpyxl is centered on transferability of data, data visualizations (e.g. charts), and some visual formatting information between Excel and python. But there is an openpyxl.chart.shapes submodule that may provide what you need.
I think you need "xlwings".
xlwing does a lot of things that openpyxl doesn't work. Keep the shapes you want or keep the formulas... However, be careful as it cannot operate in an environment without Excel.
import xlwings
app = xlwings.App(visible=False)
wb=app.books.open('test.xlsx')
ws= wbxl.sheets[0]
ws.shapes
xlwings docs

How to make python read multiple sheets on an excel file one by one

Stackoverflow
Hi python noob here.
I been learning python for couple of weeks so I don’t know if this is possible or even super easy.
I have an excel files with o lot of sheets, I have managed to create a python code that do all the changes I need to make on a single sheet and then saves it.
I just need to type the sheet name run it then type another sheet name and run it again etc …
Is there a way to make the code so it does all the sheets one by one when I run it?
I was thinking about creating a list with all the sheets name and using a loop but not sure how… thank you.
Use openpyxl module.
You have the list of sheets in openpyxl object, so you can run over sheets as by list.
You could find the solutions of similar problem here:
getting sheet names from openpyxl

Load only a single sheet from a large workbook with Python

Using Python 3.7. I have several .xlsx workbooks with 34 sheets each, most of which have conditional formatting and charts, but all I'm actually after is a cell with specified text that's somewhere on the first sheet of each book. The workbook is not protected but the sheet is, and I don't know the password, so I can't use pandas.read_excel; using openpyxl/load_workbook, it takes ages to load and I get lots of errors about it not being able to handle conditional formatting etc. I then have to search the sheet for the text.
Is there an easy, quick way of loading just the first sheet (or a named sheet)? The pandas code is very quick and easy, but I can't use it :(
Not completely sure about that but I can recommend trying "read-only" mode from openpyxl
https://openpyxl.readthedocs.io/en/stable/optimized.html
It does not fetch the full file but read it in so-called "lazy" mode. Thus you can jump to the cell you need.
It also allows to start reading from the specific sheet
Note that closing file is mandatory

Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook

I do a lot of data analysis in Excel and have been exploring Python and DataNitro to streamline my workflow. I specifically am trying to copy certain cells from one sheet in one Excel workbook, and paste them into certain cells in a certain sheet in another Excel workbook.
I have been storing ("copying") using CellRange (DataNitro), but am not sure how to copy the stored contents into a particular sheet, in another Excel workbook. Any clue how I may go about this? Also, is it possible to make the range defined for a CellRange conditional on certain cell properties?
I would really appreciate any help! Thank you, all.
Here's an example of copying:
data = CellRange("A1:A10").value
active_wkbk("Book2.xlsx")
CellRange("A1:A10").value = data
You can make the range conditional using regular Python logic (if statements, etc.).

xlsx file extension not valid after saving with openpyxl and keep_vba=true. Which is the best way?

In the environment, we have an excel file, which includes rawdata in one sheet and pivot table and charts in another sheet.
I need to append rows every day to raw data automatically using a python job.
I am not sure, but there may be some VB Script running on the front end which will refresh the pivot tables.
I used openpyxl and by following its online documentation, I was able to append rows and save the workbook. I used keep_vba=true while loading the workbook to keep the VBA modules inside to enable pivoting. But after saving the workbook, the xlsx is not being opened anymore using MS office and saying the format or the extension is not valid. I can see the data using python but with office, its not working anymore. If I don't use keep_vba=true, then pivoting is not working, only the previous values are present (ofcourse as I understood, as VBA script is needed for pivoting).
Could you explain me what's happening? I am new to python and don't know its concepts much.
How can I fix this in openpyxl or is there any better alternative other than openpyxl. Data connections in MS office is not an option for me.
As I understood, xlsx may need special modules to save the VB script to save in the same way as it may be saved using MS office. If it is, then what is the purpose of keep_vba=true ?
I would be grateful if you could explain in more detail. I would love to know.
As I have very short time to complete this task, I am looking for a quick answer here, instead of going through all the concepts.
Thankyou!
You have to save the files with the extension ".xlsm" rather than ".xlsx". The .xlsx format exists specifically to provide the user with assurance that there is no VBA code within the file. This is an Excel standard and not a problem with openpyxl. With that said, I haven't worked with openpyxl, so I'm not sure what you need to do to be sure your files are properly converted to .xlsm.
Edit: Sorry, misread your question first time around. Easiest step would be to set keep_vba=False. That might resolve your issue right there, since you're telling openpyxl to look for VBA code that can't possibly exist in an xlsx file. Hard to say more than that until you post the relevant section of your code.

Categories

Resources