Home>

I started studying Python with anaconda + VS code today.

In the book Marketing Python, the following code was introduced in the section "Automating excel routine work", so I just typed in the code manually.
Then, although it worked properly up to the glob part, it did not work from openpyxl, and the same phenomenon occurred even if I copied and executed the answer code as it was.

I found out that SyntaxError: invalid syntax is a syntax error, but I am confused because I do not know the reason because the same phenomenon occurs even if I copy and paste.

The content is to create a new file by collecting a lot of Excel files under/task2/data/in the folder directory into/task2/output /.

I am a beginner and the explanation may not be good, but I am sorry but I would appreciate it if you could answer.

I kindly thank you.

>>>C: /ProgramData/Anaconda3/python.exe c:/Users/[user name] /OneDrive/document/python/tutorial/task2/task2_excel-merge.py
  File "<stdin>", line 1
    C: /ProgramData/Anaconda3/python.exe c: /Users/[username]/OneDrive/document/python/tutorial/task2/task2_excel-merge.py
      ^ ^
SyntaxError: invalid syntax
Corresponding source code
import glob
filelist = glob.glob ('./task2/data/*.xlsx')
import openpyxl
out_workbook = openpyxl.Workbook ()
out_sheet = out_workbook.active
out_sheet.title ='Monthly report list'
out_sheet ['A1']. value ='report date'
out_sheet ['B1']. value ='branch'
out_sheet ['C1']. value ='person in charge'
out_sheet ['D1']. value ='Sales target (thousand yen)'
out_sheet ['E1']. value ='Sales record (thousand yen)'
out_sheet ['F1']. value ='Difference (thousand yen)'
out_sheet ['G1']. value ='Achievement rate (%)'
for i, file in enumerate (filelist):
    workbook = openpyxl.load_workbook (file, data_only = True)
    sheet = workbook ['business report']
    report_date = sheet ['X1']. value
    office = sheet ['X2']. value
    person = sheet ['X3']. value
    sales_goal = sheet ['H9']. value
    sales_result = sheet ['H10']. value
    sales_diff = sheet ['H11']. value
    sales_percentage = sheet ['H12']. value
    out_sheet.cell (row = i + 2, column = 1) .value = report_date
    out_sheet.cell (row = i + 2, column = 2) .value = office
    out_sheet.cell (row = i + 2, column = 3) .value = person
    out_sheet.cell (row = i + 2, column = 4) .value = sales_goal
    out_sheet.cell (row = i + 2, column = 5) .value = sales_result
    out_sheet.cell (row = i + 2, column = 6) .value = sales_diff
    out_sheet.cell (row = i + 2, column = 7) .value = sales_percentage
    out_sheet.cell (row = i + 2, column = 7) .number_format = '0%'
    out_sheet.cell (row = i + 2, column = 1) .number_format ='yyyy year mm month dd day'
out_filename ='./task2/output/ Monthly report list_202001.xlsx'
out_workbook.save (out_filename)
What I tried

nothing special

Supplementary information (FW/tool version, etc.)

anaconda python 3.8
VS CODE 1.50.0

  • Answer # 1

    There is nothing wrong with the code, but enter C: /ProgramData/Anaconda3/python.exe c: /Users/[username] /OneDrive/document/python/tutorial/task2/task2_excel-merge.py directly at the command prompt please.

  • Answer # 2

    >>>Is displayed after starting Python
    C: /ProgramData/Anaconda3/python.exe c: /Users/[username]/OneDrive/document/python/tutorial/task2/task2_excel-merge.py
    I think you are running.
    Instead, on the terminal (command prompt)
    C: /ProgramData/Anaconda3/python.exe c: /Users/[username]/OneDrive/document/python/tutorial/task2/task2_excel-merge.py
    Please run the.