I have a question about saving Python 2.x version in Excel.

Asked 1 years ago, Updated 1 years ago, 90 views

Hello, I'm an office worker who just started learning Python.

I am studying with the goal of automating Excel using Python, and I am writing because I have a question

For example,

I read "Hello Python World" in one cell in the A Excel file I made a new B Excel file and saved it in one cell

Now, here's the question.

Don't put that sentence in one cell

Put "hello" in the first row of the first row Put Python in the first row and the second row I want to put world in row 1 and row 3. Then I want to know what to do. (Because my skills are insignificant, I would appreciate it if you could explain it with an example code...))

I am using xlwt and xlrd to make the library I am using now.

I would appreciate it if you could teach me a lot

python excel save

2022-09-22 13:41

1 Answers

We write the built-in function briefly, assuming that you know it.

If you call the string A... Cut A = A.split() to make a list.

And I think you can enter it in the corresponding column as a for statement.


A = "Hello Python World" #Copy Cells
A = A.split()
Row number = 10 # For example,

for column number, value in enumerate(A):
    ws.write (row number, column number, value)

(If you're going to copy and paste multiple rows, make it a two-dimensional arrangement when you copy them.)

Working with more than one worksheet is also simple.

I brought this because there is a good example of this in the stack overflow.

import xlwt

data1 = [["a1", "b1", "c1", "d1"], ["e1", "f1", "g1", "h1"], ["i1", "j1", "k1", "l1"]]
data2 = [["a2", "b2", "c2", "d2"], ["e2", "f2", "g2", "h2"], ["i2", "j2", "k2", "l2"]]
data3 = [["a3", "b3", "c3", "d3"], ["e3", "f3", "g3", "h3"], ["i3", "j3", "k3", "l3"]]

sheets = ["Sheet 1", "Sheet 2", "Sheet 3"]

wb = xlwt.Workbook()

for sheet, data in zip(sheets, [data1, data2, data3]):
    ws = wb.add_sheet(sheet)

    for row, row_value in enumerate(data):
        for col, col_value in enumerate(row_value):
            ws.write(row, col, col_value)

wb.save('output.xls')

*Source: https://stackoverflow.com/questions/35085436/xlwt-write-multiple-columns

Personally, if you are aiming to automate the company's Excel work,

Unless there is a special reason, I think you should use Python 3 and win32com packages.

I'm also an office worker and I'm automating with win32com, but from what I've used...

It's much more comfortable than xlrd, xlwt

The biggest advantage is that you can refer to VBA macro records when you have to do complicated tasks.

You can translate the VBA macro almost exactly as it is.

When multiple cells are selected, the data is copied to a double tuple.

The downside is that you need to have an Excel file, and the cord is not pythonic?h

Below is a simple code that thickens or colors only certain characters in one cell.

It's quite intuitive, right?lol (It's possible in xlwt, too.)

import win32com.client as win32
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb1 = excel.Workbooks.Add()
ws1 = wb1.Worksheets(1)
excel.Visible = True
Specific letter = ws1.Cells (1,1).GetCharacters(Start=4,Length=7).Font
# Indexing is also starting from 1 so that it's not Pythonic...
Specific characters.ColorIndex = 4
Specific letter.Name = "Sharing Ming"
Specific characters.Size = 50
Specific letter.Bold = True
Specific letter.Shadow = True
...

Isn't it nice?h

The first question you asked me was not a for moon, but

Bulk input is also possible. You can pivot, you can graph.

Sorry if I was nosy


2022-09-22 13:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.