To enter a string from Python to Excel (xlwings) containing the 記号 symbol

Asked 1 years ago, Updated 1 years ago, 390 views

I am thinking of using Python instead of Excel VBA.
What should I do with the escape sequence (マーク mark) when I try to write a string stored in a variable on the python source that means a folder path in an Excel cell?

[Explanatory source (it simplifies the actual product)]
test = 'C:\\test'

wb = xlw.Book(myExcelBook)
sheet_pyLog = wb.sheets['sheets1']
sheet_pyLog.cells(1, 1).value = test ...1
sheet_pyLog.cells(2, 1).value = repr(test) ・・②

If so, each of the Excel cells will have a

That's what it looks like.I would like to see C:\test, what should I do?
Please consider the test on the condition that it is described.( r'C:\test' is not used.)

環境
Python 3.10.9
conda 23.1.0
xlwings 0.29.1
PyCharm 2023.1 (Community Edition)

Thank you in advance.

python python3 excel

2023-04-18 05:29

1 Answers

Is there something wrong with checking if the number of Python-related or Excel versions is out of date?

Alternatively, between the definition and configuration process of test = 'C:\\test' and the assignment of sheet_pyLog.cells(1, 1).value = test to Excel cells, there may be something that escapes the backslash character further.To simplify your questions, check to see if any of the deleted processes exist.

In the following environments, 'C:\test, 2 cells display 'C:\\test':

Package    Version
---------- -------
Python     3.10.11
pip        23.1
pywin32    306
setuptools 67.6.1
wheel      0.40.0
xlwings    0.30.4
Microsoft® Excel® for Microsoft 365 MSO (version 2303 build 16.0.16227.2020) 64-bit

Run the following script:

import xlwings as xw
wb = xw.Book()

test = 'C:\\test'

sheet = wb.sheets['Sheet1']
sheet.cells(1, 1).value = test
sheet.cells(2, 1).value = repr(test)

wb.save('FileName.xlsx')
xl = xw.apps.active
xl.quit()

Open the completed FileName.xlsx in Excel and check the cells in A1 and A2.

And I think it's part of trial and error, but what do you rate?Do you usually not use repr() to substitute?
There are differences and different ways to use them in articles around here.
Pythonのstr( )とrepr( )の使い分け
[Python]How to use and execute the repr function
【Python】__str__と__repr__の使い方
Python: __str__() and __repr__() represent objects in strings


2023-04-18 07:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.