I write a testing Python program, as follow:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet("a")
worksheet.write(0,0, "b\r\nc\r\nd")
workbook.close()
It is expected there will be a cell filled with 3 lines. But when I open it, the three letters are in one line, until I double click the cell like this:
When I download a sheet from doc.google.com, the multiline cells will shrink into one line. After double-click, it will expands.
So, is there any way I can batch expand the cells rather than double click on each one?
The following steps work:
Select all
Click the Wrap Text Button(Home - Alignment - Wrap Text )
Adapt the width of columns and height of rows if necessary.
Thanks to #BruceWayne's question in comment.
Thanks for #Slai's Answer. In a Python script, set_text_wrap() can be used to make same influnce with the action above. The full code:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet("a")
cell_format = workbook.add_format()
cell_format.set_text_wrap()
worksheet.write(0,0, "b\nc\nd", cell_format)
workbook.close()
Related
Trying to freeze the first two rows and first column with openpyxl, however, whenever doing such Excel says that the file is corrupted and there is no freeze.
Current code:
workbook = openpyxl.load_workbook(path)
worksheet = workbook[first_sheet]
freeze_panes = Pane(xSplit=2000, ySplit=3000, state="frozen", activePane="bottomRight")
worksheet.sheet_view.pane = freeze_panes
Took a look at the documentation, however, there is little explanation on parametere setting.
Desired output:
Came across this answer, however, it fits a specific use case, hence, wanted to make a general question for future reference:
How to split Excel screen with Openpyxl?
To freeze the first two rows and first column, use the sample code below... ws.freeze_panes works. Note that, like you would do in excel, select the cell above and left of which you want to freeze. So, in your case, the cell should be B3. Hope this is what you are looking for.
import openpyxl
wb=openpyxl.load_workbook('Sample.xlsx')
ws=wb['Sheet1']
mycell = ws['B3']
ws.freeze_panes = mycell
wb.save('Sample.xlsx')
Here it is answered how to change a single cell to text format. In a nutshell, I can do
from openpyxl.styles import numbers
cell.number_format = numbers.FORMAT_TEXT
Using for loops I can extend this to multiple cells.
But how can I make all cells adapt text-format? E.g. if somebody later manually puts a date like 2022-04-27 into cell CD130000, how can I ensure that the format will be of text and the date won't get auto-changed by excel?
Thanks for your help!
You should be able to use Xlwings
import xlwings as xw
wb = xw.Book('Book1.xlsx')
ws = wb.sheets('Sheet1')
### Either of the following two lines should set the whole sheet to 'Text'
ws.api.Cells.NumberFormat = '#'
ws.cells.number_format = '#'
wb.save()
wb.close()
I have the following code to append a dataframe in to a google sheet that runs everyday.
I had to create 03 more tabs in to this sheet and now, every time I upload the dataframe it goes to another tab and not the one that I need.
I`m using the following code to update the gsheet:
gc = gspread.authorize(credentials)
sh = gc.open_by_key("1O1NKT4LRf7F17kRjupUD7peonCwT04BG-l7pbo5-BLU").sheet1
values = df.values.tolist()
sh.append_rows(values)
I tried a few things such as
sh = gc.open_by_key("1O1NKT4LRf7F17kRjupUD7peonCwT04BG-l7pbo5-BLU").tabname
But it didnt work. Is there a way to do that?
thank you
Using sheet1 will give you the first worksheet in your spreadsheet, if your target sheet is not the first worksheet then you might need to use other methods to access that particular worksheet.
Best option is to get the worksheet by title (if you select worksheet using indexes, you need to update your code if ever you re-arranged your tabs. Hence the best option is to select worksheet by its title)
Here are all the options that you can use to select a worksheet using gspread:
Select worksheet by index. Worksheet indexes start from zero:
sh = gc.open_by_key("1O1NKT4LRf7F17kRjupUD7peonCwT04BG-xxxxxx")
worksheet = sh.get_worksheet(0)
Or by title:
worksheet = sh.worksheet("January")
Or the most common case: Sheet1:
worksheet = sh.sheet1
To get a list of all worksheets: (check each worksheet in the list based on their title)
worksheet_list = sh.worksheets()
I want to read in many Excel documents and I would like to receive at least one important bit of information on the format. However, I am afraid that there is no tool for it, so my hope is on you!
Each excel file that I am reading in contains a few cells that of which the content is strikethrough. For those who don't know the word (I didn't know it either), strikethrough means that there is a horizontal line through the content.
I have figured out that I will need to read in my documents with xlrd to be able to identify the fonts. However, I have been going over a list of possibilities and none of them contains a check on strikethrough.
You have to open the workbook with formatting_info kwarg as True. Then, get the XF object of the cells and get the Font object. The struck_out attribute is what you're looking for. An example:
workbook = xlrd.open_workbook(filename, formatting_info=True)
sh = workbook.sheet_by_name(sheet)
xf = workbook.xf_list[sh.cell_xf_index(row, col)]
font = workbook.font_list[xf.font_index]
if font.struck_out:
print(row, col)
from openpyxl import load_workbook
book = load_workbook('xyz.xlsx')
sheet = book.get_sheet_names()[0] #This will consider **Sheet1** of our excel file
ws = book.get_sheet_by_name(sheet)
for row in ws.iter_rows():
for cell in row:
if cell.font.strike:
print(cell.value)
this is the code that i'm trying. but its not working.
import xlsxwriter
....
sheet.merge_range.write_formula('F16:H16', """IF('Original data'!B4<>"",'Original data'!B4,"")""", center)
Is there another code that can put both of them become one? i'm already doing some research and don't get any. thanks in advance
From the docs on merge_range():
The merge_range() method writes its data argument using write(). Therefore it will handle numbers, strings and formulas as usual. If this doesn’t handle your data correctly then you can overwrite the first cell with a call to one of the other write_*() methods using the same Format as in the merged cells. See Example: Merging Cells with a Rich String.
Here is a small working example based on yours:
import xlsxwriter
workbook = xlsxwriter.Workbook('example.xlsx')
worksheet1 = workbook.add_worksheet()
worksheet2 = workbook.add_worksheet('Original data')
center = workbook.add_format({'align': 'center', 'fg_color': 'yellow'})
worksheet1.merge_range('F16:H16',
"""=IF('Original data'!B4<>"",'Original data'!B4,"")""",
center)
worksheet2.write('B4', 'Hello')
workbook.close()
Output: